IO(五、FileReader第二种读数据)char[] buf=new char[1024];

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


//通过字符数组读取
public class FileReader2 {
public static void main(String[] args) {
FileReader fr=null;
try {
fr = new FileReader("G:\\demo.txt");
//定义一个字符数组,用于存储读到字符
char[] buf=new char[1024];
/*//fr.read(buf)把读出的字符存到数组,返回的是读到的字符个数
int num=fr.read(buf);
System.out.println(num+"   "+new String(buf));*/
int num=0;
while((num=fr.read(buf))!=-1){
System.out.println(new String(buf,0,num));//从0开始取,取num个
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



}
   
}

版权声明:本文为ygl6150373原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。