字节流

字节流

字节输出流OutputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* @ClassName OutputStreamExer
* @Description 字节输出流练习
* @Author renhongchang
* @Date 2021/7/19 19:31
* @Version 1.0
* @Blog https://rhc-rgb.github.io
*/

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* Java.io.OutputStram抽象类是字节输出流的所有类的超类
* public void close() :关闭此输出流并释放与此流相关联的任何系统资源。
* public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。
* public void write(byte[] b) :将 b.length字节从指定的字节数组写入此输出流。
* public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。
* public abstract void write(int b) :将指定的字节写入输出流。
*
* 注意:
* close方法,当完成流的操作时,必须调用此方法,释放系统资源。
*/
public class OutputStreamExer {
public static void main(String[] args) throws IOException {
//写出字节
exer01();
//写出字节数组
exer02();
//写出指定长度字节数组
exer03();
//以指定名称写出数据
exer04();
//写出换行
exer05();
}

/**
* FileOutputStram 是文件输出流,用于将数据写到文件
*
* 构造方法:
* FileOutputStream(File file) 创建文件输出流以写入指定的文件对象
* FileOutputStream(String name) 创建文件输出流以指定的名称写入文件
*
* 当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有这个文件,会创建该文件。如果有这个文件,会清空这个文件的数据。
*/

/**
* 写出字节
* 一次写多个字节:
* 如果写的第一个字节是正数(0~127),那么实现的时候会查询ASCII表
* 如果写的第一个字节是负数,那第一个字节会和第二个字节,两个字节组成一个中文显示,查询默认系统码表(GBK)
* @throws IOException
*/
private static void exer01() throws IOException {
//创建文件对象
File file = new File("out.txt");
//创建文件输出流 //此处第二个参数使用 true,每次写数据不会覆盖前边数据,默认为false
FileOutputStream fileOutputStream = new FileOutputStream(file,true);
//将数据写入文件 每次写出一个字节
fileOutputStream.write(-3); //写出第一个字节
fileOutputStream.write(23); //写出第二个字节
//刷新流
fileOutputStream.flush();
//关闭流
fileOutputStream.close();
}

/**
* 写出字节数组:
*
* @throws IOException
*/
private static void exer02() throws IOException {
//创建文件对象
File file = new File("out.txt");
//创建文件输出流 //此处第二个参数使用 true,每次写数据不会覆盖前边数据,默认为false
FileOutputStream fileOutputStream = new FileOutputStream(file,true);
//字符串转换成字节数组
//getBytes(String charsetName)方法,使用指定字符集,将字符串提取成byte序列,并存储在byte数组中;
// getBytes():使用平台默认字符集
byte[] b = "java程序员".getBytes();

//写出字节数组数据
fileOutputStream.write(b);
//刷新流
fileOutputStream.flush();
//关闭流
fileOutputStream.close();
}

/**
* 写出指定长度字节数组:
*
* @throws IOException
*/
private static void exer03() throws IOException {
//创建字节输出流对象
FileOutputStream fileOutputStream = new FileOutputStream(new File("out.txt"));
//创建字节数组
byte[] bytes = "Hello world".getBytes();
//将指定长度写入文件
fileOutputStream.write(bytes,3,4);
//刷新流
fileOutputStream.flush();
//关闭流
fileOutputStream.close();
}

/**
* 以指定名称写出数据:
*
* @throws IOException
*/
private static void exer04() throws IOException {
//创建字节输出流对象
FileOutputStream fileOutputStream = new FileOutputStream("output.txt");
//写出字节数据
fileOutputStream.write(97);
//关闭流
fileOutputStream.close();
}

/**
* 写出换行:
* windows中换行符号是: \r\n
* 回车符:\r
* 换行符:\n
*
* @throws IOException
*/
private static void exer05() throws IOException {
//创建字节输出流对象
FileOutputStream fileOutputStream = new FileOutputStream(new File("out.txt"));
//创建字节数组
byte[] b = {97,98,99,100,101,102,103};
//写出换行
for (int i = 0;i < b.length;i++){
//写出一个字节
fileOutputStream.write(b[i]);
//写出一个换行,换行符号住哪换成数组写出
fileOutputStream.write("\r\n".getBytes());
}
//关闭资源
fileOutputStream.close();
}

}

字节输入流InputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* @ClassName InputStreamExer
* @Description 字节输入流练习
* @Author renhongchang
* @Date 2021/7/19 20:16
* @Version 1.0
* @Blog https://rhc-rgb.github.io
*/


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

/**
* java.io.InputStream 抽象类是表示字节输入流的所有类的超类,可以读取字节信息到内存中。它定义了字节输入流的基本共性功能方法。
* public void close() :关闭此输入流并释放与此流相关联的任何系统资源。
* public abstract int read() : 从输入流读取数据的下一个字节。
* public int read(byte[] b) : 从输入流中读取一些字节数,并将它们存储到字节数组 b中 。
* 注意:
* close方法,当完成流的操作时,必须调用此方法,释放系统资源。
*
* java.io.FileInputStream 类是文件输入流,从文件中读取字节。
* 构造方法
* FileInputStream(File file) : 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
* FileInputStream(String name) : 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
* 当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有该文件,会抛出 FileNotFoundException 。
*/

public class InputStreamExer {
public static void main(String[] args) throws IOException {
//读取字节
exer01();
//使用字节数组读取数据
exer02();
}

/**
* read 方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回 -1 ,
*
* 1、fileInputStream.read( ); 读取一个字节
* 2、i = fileInputStream.read( ); 把读取到的字节赋值给变量b
* 3、(i = fileInputStream.read( )!=-1; 判断变量b是否等于-1
*
* @throws IOException
*/
private static void exer01() throws IOException {
//创建文件对象File
File file = new File("out.txt");
//创建输入流对象
FileInputStream fileInputStream = new FileInputStream(file);
//读取字节
int i;
//read()方法必须写在循环里边
while ((i = fileInputStream.read()) != -1){
System.out.println((char) i);
}
//关闭流
fileInputStream.close();
}

private static void exer02() throws IOException {
//创建文件字节输入流对象
FileInputStream fileInputStream = new FileInputStream(new File("out.txt"));
//定义数组作为容器
byte[] bytes = new byte[2];
//定义变量作为读取的有效个数
int len;
while((len = fileInputStream.read(bytes)) != -1){
//每次读取后把数组打印成字符串
System.out.println(Arrays.toString(bytes));
}
//关闭资源
fileInputStream.close();
}

}