初学Java,IO之使用FileOutputStream和FileWriter写入文件(四十二)

来源:互联网 发布:青峰网络招聘 编辑:程序博客网 时间:2024/06/05 16:27
[java] view plaincopyprint?
  1. import java.io.*;  
  2. public class FileOutputStreamTest   
  3. {  
  4.     public static void main(String[] args) throws IOException  
  5.     {  
  6.         FileInputStream fis = null;  
  7.         FileOutputStream fos = null;  
  8.         try  
  9.         {  
  10.             //创建字节输入流  
  11.             fis = new FileInputStream("FileOutputStreamTest.java");  
  12.             //创建字节输出流  
  13.             fos = new FileOutputStream("newFile.txt");  
  14.             byte[] bbuf = new byte[32];  
  15.             int hasRead = 0;  
  16.             //循环从输入流中取出数据  
  17.             while((hasRead = fis.read(bbuf)) > 0)  
  18.             {  
  19.                 //每读取一次,即写入文件输出流,读了多少,就写多少  
  20.                 fos.write(bbuf,0,hasRead);  
  21.             }  
  22.         }  
  23.         catch (IOException ioe)  
  24.         {  
  25.             ioe.printStackTrace();  
  26.         }  
  27.         finally  
  28.         {  
  29.             if(fis != null)  
  30.             {  
  31.                 fis.close();  
  32.             }  
  33.             if(fos != null)  
  34.             {  
  35.                 fos.close();  
  36.             }  
  37.         }  
  38.     }  
  39. }  

0 0