用字符流进行纯文本的读取和写出

纯文本的读取,步骤:
1.建立联系 file对象
2.选择流: Reader FileReader
3.读取:char[] flush=new char[1024];
4.关闭资源

思路和读取文件基本是一致的,下面比较一下:

字节流读取文件VS字符流读取纯文本
1.使用流不同,前者使用“stream”,后者是“reader”
2.读取使用数组不同,前者是byte数组,后者是char数组
3.速度不同,后者速度要比前者快

 

public class Demo05 {
public static void main(String[] args) {
/**
* 纯文本读取
*/

//1.建立联系
File src=new File("F:/Picture/test/test.txt");

//2.选择流
Reader reader=null;
try {
reader=new FileReader(src);

//3.char数组读取
char[] flush=new char[1024];
int len=0;

while(-1!=(len=reader.read(flush))){
String str=new String(flush,0,len);
System.out.println(str);
}


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//4.关闭资源
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

posted on 2019-01-28 09:35  我是司  阅读(696)  评论(0编辑  收藏  举报

导航