NIO - FileChannel

转自:http://blog.csdn.net/java2000_wl/article/details/7614611

  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.nio.ByteBuffer;  
  9. import java.nio.channels.Channel;  
  10. import java.nio.channels.FileChannel;  
  11. import java.nio.charset.Charset;  
  12.   
  13. public class FileChannelMain {  
  14.       
  15.     private static final Charset charset = Charset.forName("GBK");  
  16.       
  17.     private static final int BUFFER_CAPACITY  = 1024;  
  18.       
  19.     public static void main(String[] args) throws IOException, InterruptedException {  
  20.         final String srcfilePath = "D:/tomcat-6.0.26/logs/catalina.2012-04-30.log";  
  21.         readFile(srcfilePath);  
  22.           
  23.         final String writeFilePath = "D:/test.txt";  
  24.         final String[] lines = new String[]{"line1xxssss""中文测试""!@#$%^&*()"};   
  25.         writeFile(writeFilePath, lines, Boolean.TRUE);  
  26.         readFile(writeFilePath);  
  27.           
  28.         final String targetFilePath = "D:/test-copy.txt";  
  29.         copyFile1(srcfilePath, targetFilePath);  
  30.         copyFile2(srcfilePath, targetFilePath);  
  31.     }  
  32.   
  33.     /** 
  34.      * 
  35.      * <br>------------------------------<br> 
  36.      * @param srcfilePath 
  37.      * @param targetPath 
  38.      * @throws IOException  
  39.      */  
  40.     private static void copyFile2(String srcfilePath, String targetPath) throws IOException {  
  41.         File file = new File(targetPath);  
  42.         if (!file.getParentFile().exists()) {  
  43.             file.mkdirs();  
  44.         }  
  45.         FileInputStream fileInputStream = new FileInputStream(srcfilePath);  
  46.         FileOutputStream fileOutputStream = new FileOutputStream(file);  
  47.         FileChannel inChannel = fileInputStream.getChannel();  
  48.         FileChannel outChannel = fileOutputStream.getChannel();  
  49.         //两者等价  
  50. //      inChannel.transferTo(0, inChannel.size(), outChannel);  
  51.         outChannel.transferFrom(inChannel, 0, inChannel.size());  
  52.           
  53.         close(fileOutputStream);  
  54.         close(fileInputStream);  
  55.         close(inChannel);  
  56.         close(outChannel);  
  57.     }  
  58.   
  59.     /** 
  60.      * 
  61.      * <br>------------------------------<br> 
  62.      * @param srcfilePath 
  63.      * @param targetPath 
  64.      * @throws IOException  
  65.      */  
  66.     private static void copyFile1(String srcfilePath, String targetPath) throws IOException {  
  67.         File file = new File(targetPath);  
  68.         if (!file.getParentFile().exists()) {  
  69.             file.mkdirs();  
  70.         }  
  71.         FileInputStream fileInputStream = new FileInputStream(srcfilePath);  
  72.         FileOutputStream fileOutputStream = new FileOutputStream(file);  
  73.         FileChannel inChannel = fileInputStream.getChannel();  
  74.         FileChannel outChannel = fileOutputStream.getChannel();  
  75.         ByteBuffer inBuffer = ByteBuffer.allocate(BUFFER_CAPACITY);  
  76.         while (inChannel.read(inBuffer) != -1) {  
  77.             inBuffer.flip();  
  78.             outChannel.write(inBuffer);  
  79.             inBuffer.clear();  
  80.         }  
  81.         close(fileOutputStream);  
  82.         close(fileInputStream);  
  83.         close(inChannel);  
  84.         close(outChannel);  
  85.     }  
  86.   
  87.     /** 
  88.      * <br>------------------------------<br> 
  89.      * @param writeFilePath 
  90.      * @param lines 
  91.      * @param append 
  92.      * @throws IOException 
  93.      */  
  94.     private static void writeFile(String writeFilePath, String[] lines, boolean append) throws IOException {  
  95.         File file = new File(writeFilePath);  
  96.         if (!file.getParentFile().exists()) {  
  97.             file.mkdirs();  
  98.         }  
  99.         FileOutputStream fileOutputStream = new FileOutputStream(file, append);  
  100.         FileChannel fileChannel = fileOutputStream.getChannel();  
  101.         ByteBuffer buffer = ByteBuffer.allocate(BUFFER_CAPACITY);  
  102.         for (String line : lines) {  
  103.             buffer.put(line.getBytes());  
  104.             buffer.put("\r\n".getBytes());  
  105.             buffer.flip();  
  106.             fileChannel.write(buffer);  
  107.             buffer.clear();  
  108.         }  
  109.         close(fileOutputStream);  
  110.         close(fileChannel);  
  111.     }  
  112.       
  113.     /** 
  114.      * <br>------------------------------<br> 
  115.      * @param path 
  116.      * @throws IOException 
  117.      */  
  118.     private static void readFile(String path) throws IOException {  
  119.         if (isFileNotExists(path)) {  
  120.             throw new FileNotFoundException();  
  121.         }  
  122.         FileInputStream fileInputStream = new FileInputStream(path);  
  123.         FileChannel fileChanne = fileInputStream.getChannel();  
  124.         ByteBuffer buffer = ByteBuffer.allocate(BUFFER_CAPACITY);  
  125.         while (fileChanne.read(buffer) != -1) {  
  126.             buffer.flip();  
  127.             System.out.println(charset.decode(buffer));  
  128.             buffer.clear();  
  129.         }  
  130.         close(fileInputStream);  
  131.         close(fileChanne);  
  132.     }  
  133.       
  134.     private static boolean isFileNotExists(String path) {  
  135.         File file = new File(path);  
  136.         return !file.exists();  
  137.     }  
  138.       
  139.     /** 
  140.      *  
  141.      * <br>------------------------------<br> 
  142.      * @param outputStream 
  143.      */  
  144.     private static void close(OutputStream outputStream) {  
  145.         if (outputStream == nullreturn;  
  146.         try {  
  147.             outputStream.close();  
  148.         } catch (IOException e) {  
  149.             e.printStackTrace();  
  150.         }  
  151.     }  
  152.       
  153.     /** 
  154.      *  
  155.      * <br>------------------------------<br> 
  156.      * @param channel 
  157.      */  
  158.     private static void close(Channel channel) {  
  159.         if (channel == null ) return;  
  160.         try {  
  161.             channel.close();  
  162.         } catch (IOException e) {  
  163.             e.printStackTrace();  
  164.         }  
  165.     }  
  166.       
  167.     /** 
  168.      *  
  169.      * <br>------------------------------<br> 
  170.      * @param inputStream 
  171.      */  
  172.     private static void close(InputStream inputStream) {  
  173.         if (inputStream == nullreturn;  
  174.         try {  
  175.             inputStream.close();  
  176.         } catch (IOException e) {  
  177.             e.printStackTrace();  
  178.         }  
  179.     }  

猜你喜欢

转载自ximeng1234.iteye.com/blog/2292009