本文根据文章:https://blog.csdn.net/wjp0000/article/details/117771752 进行修改
一、Java读取和存储文件数据流
Java读取文件,实际是将文件中的字节流转换成字符流输出到屏幕的过程
这里面涉及到两个类:InputStreamReader和OutputStreamWriter
InputStreamReader:将字节流转换成字符流
OutputStreamWriter:将字符流转换成字节流
InputStreamReader继承Reader类
OutputStreamWriter继承Writer类
二、FileInputStream
所有字节输入流的超类,用于字节流的读取。可以传递文件路径或File对象实例化
三、BufferedReader/FileReader/InputStreamReader
Reader是用于将文件中的内容读出来;Writer用于将字符写入到文件中去。
Reader是用于读取字符流的抽象类
1、BufferedReader/FileReader/InputStreamReader的继承关系如下:
所以通过BufferedReader/FileReader/InputStreamReader这三个方法读出来的文件,都可以直接输出字符
2、BufferedReader/FileReader/InputStreamReader的使用方法及区别
InputStreamReader:可以指定字符编码格式,入参传递InputStream对象。InputStream对象定义方法有以下两种:
System.in:读取控制台的输入内容
FileInputStream:可通过文件路径或File对象实例化
InputStreamReader inputstreamreader1 = new InputStreamReader(System.in);
InputStreamReader inputstreamreader2 = new InputStreamReader(new FileInputStream("/opt/xxx"));
InputStreamReader inputstreamreader3 = new InputStreamReader(new FileInputStream(new File("/opt/xxx")));
FileReader:入参直接传递文件路径或File对象
FileReader是InputStreamReader的子类,不能指定字符编码,其他与InputStreamReader基本类似
FileReader f1 = new FileReader("/opt/xxx.txt");
FileReader f2 = new FileReader(new File("/opt/xxx.txt"));
BufferedReader:入参有Reader对象和缓冲区大小(可不写)
从缓存区中读取字符流,提高效率;缓冲区大小:默认8192,默认不需要传递
建议都要用这个类去读取文件
BufferedReader buffered_filereader = new BufferedReader(new FileReader(filename));
BufferedReader buffered_inputstreamreader = new BufferedReader(new InputStreamReader(fileinputstream));
四、读取文件
BufferedReader对象使用readLine()方法判断字符串是否为null判断是否为文件末尾
String read;
while((read = buffered_filereader.readLine()) != null){
System.out.println(read);
}
Reader子类InputSteamReader和FileReader使用read()方法判断是否为-1,来判断是否为文件末尾
int i;
while ((i = filereader.read()) != -1){
System.out.print((char)i);
}
五、使用案例
1、InputSteamReader
String filename = "/opt/xxx.log";
FileInputStream fileinputstream = new FileInputStream(filename);
InputStreamReader inputstreamreader = new InputStreamReader(fileinputstream,"UTF-8");
int i;
while ((i = inputstreamreader.read()) != -1){
System.out.print((char)i);
}
2、FileReader
String filename = "/opt/xxx.log";
FileReader filereader = new FileReader(filename);
int i;
while ((i = filereader.read()) != -1){
System.out.print((char)i);
}
3、BufferedReader(入参FileReader)
String filename = "/opt/xxx.log";
FileReader filereader = new FileReader(filename);
BufferedReader buffered_filereader = new BufferedReader(filereader);
String read;
while((read = buffered_filereader.readLine()) != null){
System.out.println(read);
}
4、BufferedReader(入参InputStreamReader,此方式能指定读取的编码)
String filename = "/opt/xxx.log";
FileInputStream fileinputstream = new FileInputStream(filename);
InputStreamReader inputstreamreader = new InputStreamReader(fileinputstream,"UTF-8");
BufferedReader buffered_inputstreamreader = new BufferedReader(inputstreamreader);
String read;
while((read = buffered_inputstreamreader.readLine()) != null){
System.out.println(read);
}
标签:Java,read,FileReader,BufferedReader,FileInputStream,new,InputStreamReader
From: https://www.cnblogs.com/zoufh/p/17840402.html