s-day09-字符流一次读取一个字符数组

This commit is contained in:
2026-08-11 11:41:31 +08:00
parent ee69396379
commit 46e6eac9c2
@@ -0,0 +1,26 @@
package com.inmind.io_reader03;
import java.io.FileReader;
import java.io.IOException;
/*
字符流一次读取一个字符数组
int read(char[] cbuf) 将字符读入数组中,返回读取到的字符的个数
*/
public class Demo10 {
public static void main(String[] args) throws IOException {
//1.创建字符输入流
FileReader fr = new FileReader("source1.txt");
//2.调用读方法(一次读取一个字符数组)
char[] chars = new char[1024];//用来保存读取到的字符的数据
int len;//保存读取到字符的个数
while ((len = fr.read(chars)) != -1) {
//读取了几个字符,就展示几个
System.out.println(new String(chars,0,len));
}
//3.资源释放
fr.close();
System.out.println("程序结束");
}
}