java读取文件与写文件

前言

java中io流一直是一个令人头疼的东西,主要是各种流,各种缓冲器太多,不好记忆,所以感觉学起来很费劲,选择起来也比较烦恼。

本文主要针对java的io流读取数据使用io流读取文件和向文件中写数据,并根据个人经验讲解应该怎么选择和使用。

 

需要的知识点:

1.字节流和字符流选择

字节流:字节流读取的时候,读到一个字节就返回一个字节;  主要用于读取图片,MP3,AVI视频文件。

字符流:字符流使用了字节流读到一个或多个字节,如读取中文时,就会一次读取2个字节。只要是处理纯文本数据,就要优先考虑使用字符流。

 

2.处理流和节点流

节点流:和操作系统紧密连接的流。该层的与文件输入输出的操作都比较原始,没有进过优化,功能比较单一。

(FileReader,FileWriter和FileInputStream,FileOutStream都是输入节点流。)

 

处理流:该层流是对节点流的封装,对节点流中进行优化,加入缓冲器,提供更加丰富的API等。我们一般都使用处理流,在创建处理流对象的时候一般都需要节点流对象作为其构造参数。

(BufferedInPutStream,BufferedOutPutStream和BufferedWriter,BufferedReader)

对于这种带缓冲器的处理流与对应的节点流的最大区别可以参考:http://fjding.iteye.com/blog/2311233

 

本文重点讲解文件的读取和输出。

 

1. 文件的读取(节点流FileInputStream读取字节流)

 

 

Java代码
  1. public static void readFileByBytes(String fileName) {  
  2.     // 一般先创建file对象  
  3.     FileInputStream fileInput = null;  
  4.     try {  
  5.         File file = new File(fileName);  
  6.         if (!file.exists()) {  
  7.             file.createNewFile();  
  8.         }  
  9.         byte[] buffer = new byte[1024];  
  10.         fileInput = new FileInputStream(file);  
  11.         int byteread = 0;  
  12.         // byteread表示一次读取到buffers中的数量。  
  13.         while ((byteread = fileInput.read(buffer)) != -1) {  
  14.             System.out.write(buffer, 0, byteread);  
  15.         }  
  16.   
  17.     } catch (Exception e) {  
  18.         // TODO: handle exception  
  19.     } finally {  
  20.   
  21.         try {  
  22.             if (fileInput != null) {  
  23.                 fileInput.close();  
  24.             }  
  25.         } catch (IOException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.         }  
  29.   
  30.     }  
  31.   
  32. }  

 

 

 

 

2. 文件读取(节点流FileReader读取字符流)

 

 

Java代码
  1. public static void readFileByChars(String fileName) {  
  2.         FileReader reader = null;  
  3.         try {  
  4.             File file = new File(fileName);  
  5.             if (!file.exists()) {  
  6.                 file.createNewFile();  
  7.             }  
  8.             reader = new FileReader(file);  
  9.             char[] buffer = new char[1024];  
  10.             int charread = 0;  
  11.             while ((charread = reader.read(buffer)) != -1) {  
  12.                 System.out.print(buffer);  
  13.             }  
  14.         } catch (IOException e) {  
  15.             // TODO: handle exception  
  16.   
  17.         } finally {  
  18.   
  19.             try {  
  20.                 if (reader != null) {  
  21.                     reader.close();  
  22.                 }  
  23.             } catch (IOException e) {  
  24.                 // TODO Auto-generated catch block  
  25.                 e.printStackTrace();  
  26.             }  
  27.         }  
  28.   
  29.     }  

 其实FileReader也是需要FileInputStream对象转换的,要借助于InputStreamReader转换流。

 

 

3.文件的读取通过BufferedReader读取数据。

 

Java代码
  1. public static void readByBufferedReader(String fileName) {  
  2.         try {  
  3.             File file = new File(fileName);  
  4.             // 读取文件,并且以utf-8的形式写出去  
  5.             BufferedReader bufread;  
  6.             String read;  
  7.             bufread = new BufferedReader(new FileReader(file));  
  8.             while ((read = bufread.readLine()) != null) {  
  9.                 System.out.println(read);  
  10.             }  
  11.             bufread.close();  
  12.         } catch (FileNotFoundException ex) {  
  13.             ex.printStackTrace();  
  14.         } catch (IOException ex) {  
  15.             ex.printStackTrace();  
  16.         }  
  17.     }  

 

 

4.向文件中写入数据(字节流FileOutputStream)

 

Java代码
  1. public void writeByFileOutputStream() {  
  2.   
  3.         FileOutputStream fop = null;  
  4.         File file;  
  5.         String content = "This is the text content";  
  6.         try {  
  7.             file = new File("c:/newfile.txt");  
  8.             fop = new FileOutputStream(file);  
  9.             // if file doesnt exists, then create it  
  10.             if (!file.exists()) {  
  11.                 file.createNewFile();  
  12.             }  
  13.             // get the content in bytes  
  14.             byte[] contentInBytes = content.getBytes();  
  15.   
  16.             fop.write(contentInBytes);  
  17.             fop.flush();  
  18.             fop.close();  
  19.   
  20.         } catch (IOException e) {  
  21.             e.printStackTrace();  
  22.         } finally {  
  23.             try {  
  24.                 if (fop != null) {  
  25.                     fop.close();  
  26.                 }  
  27.             } catch (IOException e) {  
  28.                 e.printStackTrace();  
  29.             }  
  30.         }  
  31.     }  

 

 

5. 向文件中写数据(FileReader)【注意使用FileReader(“path”,true)可以往文件后面追加内容,否则就直接覆盖了】

 

Java代码
  1. public static void writeByFileReader() {  
  2.         try {  
  3.             String data = " This content will append to the end of the file";  
  4.   
  5.             File file = new File("javaio-appendfile.txt");  
  6.             // if file doesnt exists, then create it  
  7.             if (!file.exists()) {  
  8.                 file.createNewFile();  
  9.             }  
  10.   
  11.             // true = append file  
  12.             FileWriter fileWritter = new FileWriter(file.getName(), true);  
  13.             fileWritter.write(data);  
  14.             fileWritter.close();  
  15.   
  16.         } catch (IOException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.   
  20.     }  

 

 

6.采用BufferedWriter向文件中写数据

 

Java代码
  1. public static void writeByBufferedReader() {  
  2.         try {  
  3.   
  4.             String content = "This is the content to write into file";  
  5.             File file = new File("/users/mkyong/filename.txt");  
  6.             // if file doesnt exists, then create it  
  7.             if (!file.exists()) {  
  8.                 file.createNewFile();  
  9.             }  
  10.             FileWriter fw = new FileWriter(file, true);  
  11.             BufferedWriter bw = new BufferedWriter(fw);  
  12.             bw.write(content);  
  13.             bw.flush();  
  14.             bw.close();  
  15.   
  16.         } catch (IOException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.     }  

 

 

总结:

       对于我来说,一般使用io来操作文件比较多一些,一般都喜欢用BufferedReader和BufferedWriter,使用它们的一般流程是,先创建File对象(可以对路径和文件进行更多的操作),然后通过File对象创建FileReader(FileWriter)【当然在这里也可以通过创建InputStreamReader(new InputStream)来获取reader对象,看个人爱好】,然后通过FileReader(FileWriter)获得BufferedReader(BufferedWriter)对象。对于读入文件,还可以使用Scanner对读取的文件进行操作,这个比较方便,Scanner也是处理流,因此也是需要节点流作为其参数的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值