athrunwang

          紀元
          數據加載中……
          Java實現文件的復制和新Nio包通道的運用--Thinking in java
          首先是二進制讀取文件成為字節的代碼

            

          1. package com.bird.thinking;  
          2.   
          3. import java.io.BufferedInputStream;  
          4. import java.io.File;  
          5. import java.io.FileInputStream;  
          6. import java.io.FileNotFoundException;  
          7. import java.io.IOException;  
          8.   
          9. /** 
          10.  * @use 讀取二進制文件 
          11.  * @author Bird 
          12.  * 
          13.  */  
          14. public class BinaryFile {  
          15.     public static byte[] read(File bFile) throws FileNotFoundException{  
          16.         BufferedInputStream bf = new BufferedInputStream(new FileInputStream(bFile));//構建讀取流  
          17.         try{  
          18.             byte[] data = new byte[bf.available()];//構建緩沖區  
          19.             bf.read(data);  
          20.             return data;  
          21.         } catch (IOException e) {  
          22.             throw new RuntimeException(e);//改變成運行時異常  
          23.         }finally{  
          24.             try {  
          25.                 bf.close();  
          26.             } catch (IOException e) {  
          27.                 throw new RuntimeException(e);  
          28.             }  
          29.         }  
          30.           
          31.     }  
          32.       
          33.       
          34.     public static byte[] read(String bFile) throws FileNotFoundException{//重載構造方法  
          35.         return read(new File(bFile).getAbsoluteFile());  
          36.     }  
          37.       
          38.     public static void main(String [] args) throws FileNotFoundException{  
          39.         for(byte a: read("d://book.xml"))  
          40.             System.out.print(a);  
          41.     }  
          42. }  



          下面是包裝JAVA的控制臺輸入實現回顯功能

          1. package com.bird.thinking;  
          2.   
          3. import java.io.BufferedReader;  
          4. import java.io.IOException;  
          5. import java.io.InputStreamReader;  
          6.   
          7. /** 
          8.  * @use 從控制臺輸入并且回顯 
          9.  * @author Bird 
          10.  * 
          11.  */  
          12. public class Echo {  
          13.     public static void main(String [] args) throws IOException{  
          14.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));//將輸入流包裝成BufferedReader  
          15.         String s = null;  
          16.         while((s = stdin.readLine()) != null && s.length() !=0){  
          17.             System.out.println(s);  
          18.             if(s.equals("exit"))  
          19.                 break;  
          20.         }  
          21.     }  
          22. }  



          下面是使用NIO包的通道功能讀取并且寫入文件,并在文件的末尾追加內容

          1. package com.bird.thinking;  
          2.   
          3. import java.io.FileOutputStream;  
          4. import java.io.RandomAccessFile;  
          5. import java.nio.ByteBuffer;  
          6. import java.nio.channels.FileChannel;  
          7.   
          8. /** 
          9.  * @use 新nio包的通道讀取文件 
          10.  * @author Bird 
          11.  * 
          12.  */  
          13. public class GetChannel {  
          14.       
          15.     public static void main(String [] args) throws Exception{  
          16.         FileChannel fc = new FileOutputStream("d://bird.txt").getChannel();//建立讀取通道  
          17.         fc.write(ByteBuffer.wrap(BinaryFile.read("d://book.xml")));//獲得字節流并且通過通道寫入  
          18.         fc.close();  
          19.           
          20.         Thread.sleep(500);//等待磁盤寫入數據完畢  
          21.           
          22.         fc = new RandomAccessFile("d://bird.txt","rw").getChannel();//隨機讀取,對文件末尾追加內容  
          23.         fc.position(fc.size());//調整文件指針的位置到文件的末尾  
          24.         fc.write(ByteBuffer.wrap("哥再加一點".getBytes()));//在文件末尾加入這幾個字  
          25.         fc.close();       
          26.     }  
          27.       
          28.   
          29. }  



          下面是對文件的復制

          1. package com.bird.thinking;  
          2.   
          3. import java.io.FileInputStream;  
          4. import java.io.FileOutputStream;  
          5. import java.io.IOException;  
          6. import java.nio.ByteBuffer;  
          7. import java.nio.channels.FileChannel;  
          8.   
          9. /** 
          10.  *  
          11.  * @author Bird 
          12.  * @use 文件的復制 
          13.  */  
          14. public class ChannelCopy {  
          15.     private static final int BSIZE = 1024;//文件緩沖字節區,大小可以自己定  
          16.     public static void main(String [] args) throws IOException{  
          17.         FileChannel in = new FileInputStream("d://book.xml").getChannel();//得到輸入通道  
          18.         FileChannel out = new FileOutputStream("d://bird.xml").getChannel();//得到輸出通道  
          19.         ByteBuffer buffer = ByteBuffer.allocate(BSIZE);//設定緩沖區  
          20.         while(in.read(buffer) != -1){  
          21.             buffer.flip();//準備寫入,防止其他讀取,鎖住文件  
          22.             out.write(buffer);  
          23.             buffer.clear();//準備讀取。將緩沖區清理完畢,移動文件內部指針  
          24.         }  
          25.     }  

          posted on 2012-01-02 14:19 AthrunWang 閱讀(597) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 镇江市| 东莞市| 神农架林区| 漠河县| 化州市| 新巴尔虎左旗| 商城县| 绥芬河市| 合肥市| 邢台县| 浦县| 南丹县| 南京市| 瓮安县| 老河口市| 讷河市| 甘孜县| 湖州市| 罗江县| 太原市| 涡阳县| 镇巴县| 克什克腾旗| 长顺县| 苗栗县| 特克斯县| 丰城市| 新乡县| 耒阳市| 邵东县| 丰顺县| 石门县| 德钦县| 额敏县| 大同县| 康乐县| 沈丘县| 婺源县| 沧州市| 普兰县| 随州市|