athrunwang

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

            

          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()];//構建緩沖區(qū)  
          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的控制臺輸入實現(xiàn)回顯功能

          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包的通道功能讀取并且寫入文件,并在文件的末尾追加內(nèi)容

          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")));//獲得字節(jié)流并且通過通道寫入  
          18.         fc.close();  
          19.           
          20.         Thread.sleep(500);//等待磁盤寫入數(shù)據(jù)完畢  
          21.           
          22.         fc = new RandomAccessFile("d://bird.txt","rw").getChannel();//隨機讀取,對文件末尾追加內(nèi)容  
          23.         fc.position(fc.size());//調(diào)整文件指針的位置到文件的末尾  
          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;//文件緩沖字節(jié)區(qū),大小可以自己定  
          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);//設定緩沖區(qū)  
          20.         while(in.read(buffer) != -1){  
          21.             buffer.flip();//準備寫入,防止其他讀取,鎖住文件  
          22.             out.write(buffer);  
          23.             buffer.clear();//準備讀取。將緩沖區(qū)清理完畢,移動文件內(nèi)部指針  
          24.         }  
          25.     }  

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


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 汕尾市| 十堰市| 温泉县| 新乡县| 汕尾市| 固始县| 东乡县| 聂拉木县| 明光市| 荥经县| 石河子市| 和林格尔县| 灵山县| 南投市| 黔南| 安义县| 舒兰市| 托里县| 巩义市| 萍乡市| 平乡县| 新竹市| 宾川县| 乐昌市| 兴和县| 德江县| 长岛县| 巫山县| 鄱阳县| 巴中市| 贡觉县| 寿阳县| 长葛市| 武威市| 泰兴市| 洛阳市| 利津县| 霞浦县| 进贤县| 秦安县| 富源县|