Java NIO複製檔案功能

LuckyJiang-2019發表於2017-02-03
  1. 	1. package com.ghb.crp.file;   
    	2.   
    	3. import java.io.BufferedInputStream;   
    	4. import java.io.BufferedOutputStream;   
    	5. import java.io.File;   
    	6. import java.io.FileInputStream;   
    	7. import java.io.FileOutputStream;   
    	8. import java.nio.ByteBuffer;   
    	9. import java.nio.channels.FileChannel;   
    	10.   
    	11. public class FileCopy {   
    	12.     public static String src = "f://ss.rmvb";   
    	13.     public static String dist = "f://ss2.rmvb";   
    	14.   
    	15.     /**  
    	16.      * @param args  
    	17.      */  
    	18.     public static void main(String[] args) {   
    	19.         long start = System.currentTimeMillis();   
    	20.         copyNew();   
    	21.         long end = System.currentTimeMillis();   
    	22.         System.out.println("nio 用時: " + (end - start));   
    	23.   
    	24.         long start2 = System.currentTimeMillis();   
    	25.         copyOld();   
    	26.         end = System.currentTimeMillis();   
    	27.         System.out.println("io 用時: " + (end - start2));   
    	28.     }   
    	29.   
    	30.     public static void copyOld() {   
    	31.         File srcFile = new File(src);   
    	32.         File distFile = new File(dist);   
    	33.         if (distFile.exists()) {   
    	34.             distFile.delete();   
    	35.         }   
    	36.         try {   
    	37.             FileInputStream fin = new FileInputStream(srcFile);   
    	38.             BufferedInputStream buffIn = new BufferedInputStream(fin);   
    	39.   
    	40.             FileOutputStream fout = new FileOutputStream(distFile);   
    	41.             BufferedOutputStream buffout = new BufferedOutputStream(fout);   
    	42.             byte[] bytes = new byte[1024];   
    	43.             while (buffIn.read(bytes) > 0) {   
    	44.                 buffout.write(bytes);   
    	45.             }   
    	46.             fin.close();   
    	47.             buffIn.close();   
    	48.             fout.close();   
    	49.             buffout.close();   
    	50.         } catch (Exception e) {   
    	51.             e.printStackTrace();   
    	52.         }   
    	53.   
    	54.     }   
    	55.   
    	56.     public static void copyNew() {   
    	57.         try {   
    	58.             File srcFile = new File(src);   
    	59.             File distFile = new File(dist);   
    	60.             if (distFile.exists()) {   
    	61.                 distFile.delete();   
    	62.             }   
    	63.   
    	64.             FileInputStream fin = new FileInputStream(srcFile);   
    	65.             FileOutputStream fout = new FileOutputStream(distFile);   
    	66.             FileChannel inChannel = fin.getChannel();   
    	67.             FileChannel outChannel = fout.getChannel();   
    	68.             int ByteBufferSize = 1024 * 100;   
    	69.             ByteBuffer buff = ByteBuffer.allocate(ByteBufferSize);   
    	70.   
    	71.             while (inChannel.read(buff) > 0) {   
    	72.                 buff.flip();   
    	73.                 if (inChannel.position() == inChannel.size()) {// 判斷是不是最後一段資料   
    	74.                     int lastRead = (int) (inChannel.size() % ByteBufferSize);   
    	75.                     byte[] bytes = new byte[lastRead];   
    	76.                     buff.get(bytes, 0, lastRead);   
    	77.                     outChannel.write(ByteBuffer.wrap(bytes));   
    	78.                     buff.clear();   
    	79.                 } else {   
    	80.                     outChannel.write(buff);   
    	81.                     buff.clear();   
    	82.                 }   
    	83.             }// 這個使用FileChannel 自帶的複製   
    	84.             // outChannel.transferFrom(inChannel, 0, inChannel.size());   
    	85.             outChannel.close();   
    	86.             inChannel.close();   
    	87.             fin.close();   
    	88.             fout.close();   
    	89.         } catch (Exception e) {   
    	90.             e.printStackTrace();   
    	91.         }   
    	92.     }   
    	93. }  
    
    


相關文章