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 阅读(589) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: