利用输入输出流复制文件

[java] view plain copy
 
  1. package com.jn.test;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8.   
  9. /* 
  10.  * 利用输入输出流复制文件 
  11.  */  
  12. public class StreamTest {  
  13.   
  14.     public static void main(String[] args) {  
  15.         // TODO Auto-generated method stub  
  16.         File file = new File("d:/彩虹.txt");  
  17.         FileInputStream fis = null;  
  18.         FileOutputStream fos = null;  
  19.         try {  
  20.             fis = new FileInputStream(file);  
  21.             fos = new FileOutputStream("d:/彩虹2.txt");  
  22.             int len = 0;  
  23.             byte[] buf = new byte[1024];  
  24.             String s;  
  25.             while ((len = fis.read(buf)) != -1) {  
  26.                 s = new String(buf, 0, len, "GBK");  
  27.                 fos.write(buf);  
  28.             }  
  29.             // 关资源  
  30.             fis.close();  
  31.             fos.close();  
  32.         } catch (FileNotFoundException e) {  
  33.             // TODO Auto-generated catch block  
  34.             e.printStackTrace();  
  35.         } catch (IOException e) {  
  36.             // TODO: handle exception  
  37.         } finally {  
  38.             if (null != fis) {  
  39.                 try {  
  40.                     fis.close();  
  41.                 } catch (IOException e) {  
  42.                     // TODO Auto-generated catch block  
  43.                     fis = null;  
  44.                 }  
  45.             }  
  46.             if (null != fos) {  
  47.                 try {  
  48.                     fos.close();  
  49.                 } catch (IOException e) {  
  50.                     // TODO Auto-generated catch block  
  51.                     fos = null;  
  52.                 }  
  53.             }  
  54.         }  
  55.     }  
  56. }  
posted @ 2018-02-28 15:38  纯洁的赤子之心  阅读(219)  评论(0)    收藏  举报