Skip to content

[!quote] IO 流 IO 流【input,output】是用来处理设备之间的数据传输问题的,input 是从硬盘中读取数据,output 是存东西到硬盘上

字节流

[!quote] 字节流 字节流 是以字节为单位进行数据传输的流,可以处理任何类型的数据,不考虑字符的编码

OutputStream

[!quote] OutputStreamOutputStream 是字节输出流的所有类的超类

FileOutputStream

  • void write(int b) 一次写一个指定的字节数据
  • void write(byte[] b) 一次写一个字节数组数据
  • void write(byte[] b, int off, int len) 一次写一个字节数组的部分数据,从 off 开始,写 len 个长度
java
public class File {
    public static void main(String[] args) throws IOException {
        FileOutputStream fops = new FileOutputStream("E:\\文档\\学校\\zazaza.txt");
        
        // 一个一个写入
        fops.write(97);
        fops.write(98);
        fops.write(99);
        fops.write(100);
        fops.write(101);

		String line1 = "This is line 1\r\n";
		fops.write(line1.getBytes());

		// 写入一个字节数组
        byte by[] = {97, 98, 99, 100, 101};
        fops.write(by);
        
        String s1 = "green";
        fops.write(s1.getBytes());

		// 写入部分字节数组
        String s2 = "greenteck";
        fops.write(s2.getBytes(), 2, 4);

		// 写完之后释放资源
        fops.close();
    }
}

---
zazaza.txt文件内容:
abcdeThis is line 1
abcdegreenente

[!hint] 追加写入 输出流默认在打开流时,会覆盖文件的内容进行写入,如果想实现追加写入,我们要使用另一种构造方法:

java
FileOutputStream fops = new FileOutputStream("E:\\文档\\学校\\zazaza.txt", true);

[!hint] 在 finally 块 中释放流 因为我们在使用 IO 流时一定要释放资源,但是当我们 try...catch 时抛出异常后不会释放资源,所以我们引入 finally 块 用于释放资源

java
// 不能定义在 try 里,因为finally识别不到
FileOutputStream fops = null;

try {
	fops = new FileOutputStream("E:\\文档\\学校\\zazaza.txt");
	fops.write("hello".getBytes());
} catch (IOException ioE) {
	ioE.printStackTrace();
} finally {
	if (fops != null) {
		try {
			fops.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

BufferedOutputStream

[!quote] BufferedOutputStreamBufferedOutputStream 是字节缓冲流,字节缓冲流在读写数据时,内部有一个缓冲区,可以减少 I/O 操作次数,提高性能

java
FileOutputStream fops = new FileOutputStream("E:\\文档\\学校\\zazaza.txt");

// 创建字节缓冲流需要字节流对象
BufferedOutputStream bops = new BufferedOutputStream(fops);
bops.write("hello\n".getBytes());
bops.write("world".getBytes());
bops.close();

InputStream

[!quote] InputStreamInputStream 是字节输入流的所有类的超类

FileIntputStream

  • int read() 读取数据,如果文件到达末尾,则返回 -1 ;如果文件没有到达末尾,返回读取到的字节所表示的整数
  • int read(byte[] b) 读取文件数据到字节数组中,返回读到的字节长度

  • 一次读取一个数据
java
FileInputStream fips = new FileInputStream("E:\\文档\\学校\\zazaza.txt");
while (true) {
	int i = fips.read();
	if (i == -1) {
		break;
	} else {
		System.out.print((char) i);
	}
}
fips.close();

---
hello
  • 一次读取一个数组
java
FileInputStream fips = new FileInputStream("E:\\文档\\学校\\zazaza.txt");

byte b[] = new byte[5];
int len = fips.read(b);
System.out.println(len);

byte b2[] = new byte[5];
fips.read(b2);

for (byte y : b) {
	System.out.print((char) y);
}
for (byte y : b2) {
	System.out.print((char) y);
}

fips.close();
fops.close();

---
zazaza.txt文件内容:
hello
world

---
5
hello
wor    // 有两个字符是回车符和换行符

BufferedIntputStream

java
FileInputStream fips = new FileInputStream("E:\\文档\\学校\\zazaza.txt");

BufferedInputStream bips = new BufferedInputStream(fips);

while (true) {
	int i = bips.read();
	if (i!= -1) {
		System.out.print((char) i);
	} else {
		break;
	}
}

bips.close();

字符流

[!quote] 字符流 字符流 = 字节流 + 字符编码/解码 字符流 是以字符为单位进行数据传输的流,能够正确处理字符数据,和文本数据