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

This commit is contained in:
xx
2026-10-23 11:46:41 +08:00
parent c743b79d73
commit 9354b2872d
2 changed files with 21 additions and 1 deletions
@@ -15,7 +15,6 @@ public class Demo07 {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
//1.创建对象 //1.创建对象
FileInputStream fis = new FileInputStream("source1.txt"); FileInputStream fis = new FileInputStream("source1.txt");
byte[] bytes = new byte[1024];
//2.调用读方法 //2.调用读方法
//一次读取一个字节数组,返回值是读取到的字节个数 //一次读取一个字节数组,返回值是读取到的字节个数
//一次读几个字节,就获取几个字节数据 //一次读几个字节,就获取几个字节数据
@@ -31,6 +30,7 @@ public class Demo07 {
System.out.println(new String(bytes,0,len)); System.out.println(new String(bytes,0,len));
System.out.println(len);*/ System.out.println(len);*/
byte[] bytes = new byte[1024];
int len;//记录读取的字节数量,以及-1结束条件 int len;//记录读取的字节数量,以及-1结束条件
while ((len = fis.read(bytes))!=-1) { while ((len = fis.read(bytes))!=-1) {
System.out.println(new String(bytes,0,len)); System.out.println(new String(bytes,0,len));
@@ -0,0 +1,20 @@
package com.inmind.reader03;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//请大家一次读取一个字符数组的内容,读取srouce2.txt
//int read(char[] cbuf) 将字符读入数组。
public class Demo11 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("source2.txt");
char[] chars = new char[1024];
//int len = fr.read(chars);
int len;
while ((len = fr.read(chars))!= -1){
System.out.println(new String(chars,0,len));
}
fr.close();
}
}