IO流复制文件11种方法

1.IO流复制文件方法汇总


①字节流复制文件(FileInputStream/FileOutputStream和BufferedInputSream/BufferedOutputStream)

②字符流复制文件(InputStreamReader/OutputStreamWriter、FileReader/FileWriter、BufferedReader/BufferedWriter)

BufferedReader/BufferedWriter字符缓冲区流有三种方法,多了一种可以直接读取一行,一次写入一个字符串。其它都是两种方法。


2.代码体现


[java]  view plain  copy
  1. package cn.jason04;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.BufferedReader;  
  6. import java.io.BufferedWriter;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileOutputStream;  
  9. import java.io.FileReader;  
  10. import java.io.FileWriter;  
  11. import java.io.IOException;  
  12. //InputStreamReader是FileReader的父亲,FileReader是InputStreamReader的简化版  
  13. import java.io.InputStreamReader;  
  14. import java.io.OutputStreamWriter;  
  15.   
  16. //复制文件11种方法  
  17. public class Test01 {  
  18.     public static void main(String[] args) throws IOException {  
  19.         // 字节流  
  20.         FileInputStream fis = new FileInputStream("a.txt");  
  21.         FileOutputStream fos = new FileOutputStream("f.txt");  
  22.         // 字节缓冲区流  
  23.         BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));  
  24.         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f.txt"));  
  25.   
  26.         // 字符流  
  27.         InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));  
  28.         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("f.txt"));  
  29.   
  30.         FileReader fr = new FileReader("a.txt");  
  31.         FileWriter fw = new FileWriter("f.txt");  
  32.         // 字符缓冲区流  
  33.         BufferedReader br = new BufferedReader(new FileReader("a.txt"));  
  34.         BufferedWriter bw = new BufferedWriter(new FileWriter("f.txt"));  
  35.   
  36.         // 字节流方法  
  37.         copyFiles1(fis, fos);  
  38.         copyFiles2(fis, fos);  
  39.   
  40.         copyFiles3(bis, bos);  
  41.         copyFiles4(bis, bos);  
  42.   
  43.         // 字符流读取方法  
  44.         copyFiles5(isr, osw);  
  45.         copyFiles6(isr, osw);  
  46.   
  47.         copyFiles7(fr, fw);  
  48.         copyFiles8(fr, fw);  
  49.   
  50.         // 高效方法  
  51.         copyFiles9(br, bw);  
  52.         copyFiles10(br, bw);  
  53.   
  54.         // 特效方法  
  55.         copyFiles11(br, bw);  
  56.     }  
  57.   
  58.     // 字节读取方法  
  59.     private static void copyFiles1(FileInputStream fis, FileOutputStream fos) throws IOException {  
  60.         // 一次读取一个字节  
  61.         int bys = 0;  
  62.         while ((bys = fis.read()) != -1) {  
  63.             fos.write(bys);  
  64.         }  
  65.         // 关闭流  
  66.         fos.close();  
  67.         fis.close();  
  68.   
  69.     }  
  70.   
  71.     private static void copyFiles2(FileInputStream fis, FileOutputStream fos) throws IOException {  
  72.         // 一次读取一个字节数组  
  73.         byte[] bys = new byte[1024];  
  74.         int len = 0;  
  75.         // 读一个字节数组  
  76.         while ((len = fis.read(bys)) != -1) {  
  77.             fos.write(bys, 0, len);  
  78.         }  
  79.         // 关闭流  
  80.         fos.close();  
  81.         fis.close();  
  82.   
  83.     }  
  84.   
  85.     private static void copyFiles3(BufferedInputStream bis, BufferedOutputStream bos) throws IOException {  
  86.         // 一次读取一个字节  
  87.         int bys = 0;  
  88.         while ((bys = bis.read()) != -1) {  
  89.             bos.write(bys);  
  90.         }  
  91.     }  
  92.   
  93.     private static void copyFiles4(BufferedInputStream bis, BufferedOutputStream bos) throws IOException {  
  94.         // 一次读取一个字节数组  
  95.         byte[] bys = new byte[1024];  
  96.         int len = 0;  
  97.         while ((len = bis.read(bys)) != -1) {  
  98.             bos.write(bys, 0, len);  
  99.             bos.flush();  
  100.         }  
  101.         // 关闭流  
  102.         bos.close();  
  103.         bis.close();  
  104.   
  105.     }  
  106.   
  107.     // 字符读取方法  
  108.     // 普通方法  
  109.     // 方式1  
  110.     public static void copyFiles5(InputStreamReader isr, OutputStreamWriter osw) throws IOException {  
  111.         // 一次读取一个字符  
  112.         // InputStreamReader是FileReader的父类  
  113.         int ch = 0;  
  114.         while ((ch = isr.read()) != -1) {  
  115.             osw.write(ch);  
  116.             osw.flush();  
  117.         }  
  118.   
  119.         isr.close();  
  120.         osw.close();  
  121.     }  
  122.   
  123.     // 方式2  
  124.     public static void copyFiles6(InputStreamReader isr, OutputStreamWriter osw) throws IOException {  
  125.         // 一次读取一个字符数组  
  126.         char[] ch = new char[1024];  
  127.         int len = 0;  
  128.         while ((len = isr.read(ch)) != -1) {  
  129.             osw.write(ch, 0, len);  
  130.             osw.flush();  
  131.         }  
  132.   
  133.         isr.close();  
  134.         osw.close();  
  135.     }  
  136.   
  137.     // 方法3  
  138.     public static void copyFiles7(FileReader fr, FileWriter fw) throws IOException {  
  139.         // 普通方法一次读写一个字符  
  140.         int ch = 0;  
  141.         while ((ch = fr.read()) != -1) {  
  142.             fw.write(ch);  
  143.             fw.flush();  
  144.         }  
  145.   
  146.         fw.close();  
  147.         fr.close();  
  148.     }  
  149.   
  150.     // 方法4  
  151.     public static void copyFiles8(FileReader fr, FileWriter fw) throws IOException {  
  152.   
  153.         // 普通方法一次读取一个字符数组  
  154.         char[] chs = new char[1024];  
  155.         int len = 0;  
  156.         while ((len = fr.read(chs)) != -1) {  
  157.             fw.write(chs, 0, len);  
  158.             fw.flush();  
  159.         }  
  160.   
  161.         fw.close();  
  162.         fr.close();  
  163.     }  
  164.   
  165.     // 高效方法  
  166.     // 方法5  
  167.     public static void copyFiles9(BufferedReader br, BufferedWriter bw) throws IOException {  
  168.         // 高效一次读取一个字符  
  169.         int ch = 0;  
  170.         while ((ch = br.read()) != -1) {  
  171.             bw.write(ch);  
  172.             bw.flush();  
  173.         }  
  174.   
  175.         br.close();  
  176.         bw.close();  
  177.     }  
  178.   
  179.     // 方法6  
  180.     public static void copyFiles10(BufferedReader br, BufferedWriter bw) throws IOException {  
  181.         // 高效一次读取一个字符数组  
  182.         char[] chs = new char[1024];  
  183.         int len = 0;  
  184.         while ((len = br.read(chs)) != -1) {  
  185.             bw.write(chs, 0, len);  
  186.             bw.flush();  
  187.         }  
  188.   
  189.         br.close();  
  190.         bw.close();  
  191.     }  
  192.   
  193.     // 特效方法  
  194.     // 方法7  
  195.     public static void copyFiles11(BufferedReader br, BufferedWriter bw) throws IOException {  
  196.         // 特效方式  
  197.         String string = null;  
  198.         // 一次读写一行  
  199.         while ((string = br.readLine()) != null) {  
  200.             bw.write(string);  
  201.             bw.newLine();  
  202.             bw.flush();  
  203.         }  
  204.         // 关闭输入输出流  
  205.         br.close();  
  206.         bw.close();  
  207.     }  
  208.   
  209. }  

3.归纳总结

1.IO流之间关系要明确,那些流有特殊用法要明确,基本流一定要掌握。

2.读文件的时候,文件目录一定要存在,否则会抛出异常,文件目录不存在。

3.写文件的时候,会自动创建文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值