file,fileInputStream, fileReader,inputStreamReader等java文件流类的关系区别

1. File类 
1)File类介绍
File类封装了对用户机器的文件系统进行操作的功能。例如,可以用File类获得文件上次修改的时间移动,或者对文件进行删除、重命名。换句话说,流类关注的是文件内容,而File类关注的是文件在磁盘上的存储。 
File类的主要方法有:getName(),getCanonicalFile(),lastModified(),isDerector(),isFile(),getPath()等;

2)File类与FileInputStream类的区别: 
流类关注的是文件内容,而File类关注的是文件在磁盘上的存储。

File不属于文件流,只能代表一个文件或是目录的路径名而已。


FileInputStream类或者FileReader类的构造函数有多个,其中典型的两个分别为:

一个使用File对象为参数;

而另一个使用表示路径的String对象作为参数;

自己以前一直觉得直接用了String指定路径就可以了,一直不明白为什么很多人都先构造一个File对象,现在终于明白了,“如果处理文件或者目录名,就应该使用File对象,而不是字符串。”


2. FileInputStream类 
1)FileInputStream类介绍: 
以字节为单位(非unicode)的流处理。字节序列即:二进制数据。与编码无关,不存在乱码问题。 
FileInputStream类的主要方法有: 
Read(),read(byte[] b),read(byte[],int off,int len),available();



2)FileInputStream类与FileReader类的区别: 
两个类的构造函数的形式和参数都是相同的,参数为File对象或者表示路径的String,它们到底有何区别呢? 


FileInputStream:以字节流方式读取;

FileReader:把文件转换为字符流读入; 

InputStream提供的是字节流的读取,而非文本读取,这是和Reader类的根本区别。

用Reader读取出来的是char数组或者String ,使用InputStream读取出来的是byte数组。

Reader类及其子类提供的字符流的读取char(16位,unicode编码),inputStream及其子类提供字节流的读取byte(8位),所以FileReader类是将文件按字符流的方式读取,FileInputStream则按字节流的方式读取文件;

InputStreamReader可以将读如stream转换成字符流方式,是reader和stream之间的桥梁;


 最初Java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类。 

 FileInputStream类以二进制输入/输出,I/O速度快且效率搞,但是它的read()方法读到的是一个字节(二进制数据),很不利于人们阅读。 

 而FileReader类弥补了这个缺陷,可以以文本格式输入/输出,非常方便;比如可以使用while((ch = filereader.read())!=-1 )循环来读取文件;

可以使用BufferedReader的readLine()方法一行一行的读取文本。 


 当我们读写文本文件的时候,采用Reader是非常方便的,比如FileReader, InputStreamReader和BufferedReader。

其中最重要的类是InputStreamReader,它是字节转换为字符的桥梁。 

你可以在构造器重指定编码的方式,如果不指定的话将采用底层操作系统的默认编码方式,例如GBK等。 


而FileInputStream以二进制方式处理,不会出现乱码。


3)

如果处理纯文本文件,建议使用FileReader,因为更方便,也更适合阅读;但是要注意编码问题。

 其他情况(处理非纯文本文件),FileInputStream是唯一的选择;FileInputStream是进Socket通讯时会用到很多,如将文件流是Stream的方式传向服务器。



3. FileReader类 

1) FileReader类介绍: 

InputStreamReader类的子类,所有方法(read()等)都从父类InputStreamReader中继承而来; 


2) 与InputStreamReader类的区别: 

该类与它的父类InputStreamReader的主要不同在于构造函数,从InputStreamReader的构造函数中看到,参数为InputStream和编码方式,可以看出,当要指定编码方式时,必须使用InputStreamReader类;

而FileReader构造函数的参数与FileInputStream同,为File对象或表示path的String,可以看出,当要根据File对象或者String读取一个文件时,用FileReader;


3) 一般用法: 
FileReader fr = new FileReader("ming.txt"); 
  char[] buffer = new char[1024]; 
  int ch = 0; 
  while((ch = fr.read())!=-1 ) 
  { 
   System.out.print((char)ch); 

  } 


4. InputStreamReader类 
以文本格式输入/输出,可以指定编码格式; 
 主要方法: 
getEncoding(),read(); 
 一般用法: 
InputStreamReader isr = new InputStreamReader(new FileInputStream("ming.txt")); 
  while((ch = isr.read())!=-1) 
  { 
   System.out.print((char)ch); 

  } 


5. BufferedReader类 

 BufferedReader 由Reader类扩展而来,提供通用的缓冲方式文本读取,而且提供了很实用的readLine,读取分行文本很适合,BufferedReader是针对Reader的,不直接针对文件,也不是只针对文件读取。 


 一般用法: 
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ming.txt"))); 
  String data = null; 
  while((data = br.readLine())!=null) 
  { 
   System.out.println(data); 

  } 


6. 总结以上内容,得出比较好的规范用法: 


1) File file = new File ("hello.txt"); 
FileInputStream in=new FileInputStream(file); 
2) File file = new File ("hello.txt"); 
FileInputStream in=new FileInputStream(file); 
InputStreamReader inReader=new InputStreamReader(in); 
BufferedReader bufReader=new BufferedReader(inReader); 
3) File file = new File ("hello.txt"); 
FileReader fileReader=new FileReader(file); 
BufferedReader bufReader=new BufferedReader(fileReader);
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is bad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值