Java實現文件的復制和新Nio包通道的運用--Thinking in java
首先是二進制讀取文件成為字節的代碼
- package com.bird.thinking;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- /**
- * @use 讀取二進制文件
- * @author Bird
- *
- */
- public class BinaryFile {
- public static byte[] read(File bFile) throws FileNotFoundException{
- BufferedInputStream bf = new BufferedInputStream(new FileInputStream(bFile));//構建讀取流
- try{
- byte[] data = new byte[bf.available()];//構建緩沖區
- bf.read(data);
- return data;
- } catch (IOException e) {
- throw new RuntimeException(e);//改變成運行時異常
- }finally{
- try {
- bf.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
- public static byte[] read(String bFile) throws FileNotFoundException{//重載構造方法
- return read(new File(bFile).getAbsoluteFile());
- }
- public static void main(String [] args) throws FileNotFoundException{
- for(byte a: read("d://book.xml"))
- System.out.print(a);
- }
- }
下面是包裝JAVA的控制臺輸入實現回顯功能
- package com.bird.thinking;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- /**
- * @use 從控制臺輸入并且回顯
- * @author Bird
- *
- */
- public class Echo {
- public static void main(String [] args) throws IOException{
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));//將輸入流包裝成BufferedReader
- String s = null;
- while((s = stdin.readLine()) != null && s.length() !=0){
- System.out.println(s);
- if(s.equals("exit"))
- break;
- }
- }
- }
下面是使用NIO包的通道功能讀取并且寫入文件,并在文件的末尾追加內容
- package com.bird.thinking;
- import java.io.FileOutputStream;
- import java.io.RandomAccessFile;
- import java.nio.ByteBuffer;
- import java.nio.channels.FileChannel;
- /**
- * @use 新nio包的通道讀取文件
- * @author Bird
- *
- */
- public class GetChannel {
- public static void main(String [] args) throws Exception{
- FileChannel fc = new FileOutputStream("d://bird.txt").getChannel();//建立讀取通道
- fc.write(ByteBuffer.wrap(BinaryFile.read("d://book.xml")));//獲得字節流并且通過通道寫入
- fc.close();
- Thread.sleep(500);//等待磁盤寫入數據完畢
- fc = new RandomAccessFile("d://bird.txt","rw").getChannel();//隨機讀取,對文件末尾追加內容
- fc.position(fc.size());//調整文件指針的位置到文件的末尾
- fc.write(ByteBuffer.wrap("哥再加一點".getBytes()));//在文件末尾加入這幾個字
- fc.close();
- }
- }
下面是對文件的復制
- package com.bird.thinking;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import java.nio.channels.FileChannel;
- /**
- *
- * @author Bird
- * @use 文件的復制
- */
- public class ChannelCopy {
- private static final int BSIZE = 1024;//文件緩沖字節區,大小可以自己定
- public static void main(String [] args) throws IOException{
- FileChannel in = new FileInputStream("d://book.xml").getChannel();//得到輸入通道
- FileChannel out = new FileOutputStream("d://bird.xml").getChannel();//得到輸出通道
- ByteBuffer buffer = ByteBuffer.allocate(BSIZE);//設定緩沖區
- while(in.read(buffer) != -1){
- buffer.flip();//準備寫入,防止其他讀取,鎖住文件
- out.write(buffer);
- buffer.clear();//準備讀取。將緩沖區清理完畢,移動文件內部指針
- }
- }
- }
posted on 2012-01-02 14:19 AthrunWang 閱讀(597) 評論(0) 編輯 收藏