用filechannel更方便一些

 
  
  1. /**  
  2. Copy files, using two techniques, FileChannels and streams. 
  3. Using FileChannels is usually faster than using streams. 
  4. */ 
  5. public final class CopyFiles { 
  6.   
  7.  /* Change these settings before running this class. */ 
  8.   
  9.  /** The file to be copied. */ 
  10.  public static final String INPUT_FILE = "C:\\TEMP\\cottage.jpg"
  11.   
  12.  /** 
  13.   The name of the copy to be created by this class.   
  14.   If this file doesn't exist, it will be created, along with any  
  15.   needed parent directories.   
  16.  */ 
  17.  public static final String COPY_FILE_TO = "C:\\TEMP10\\cottage_2.jpg"
  18.   
  19.  /** Run the example. */ 
  20.  public static void main(String... aArgs) throws IOException{ 
  21.    File source = new File(INPUT_FILE); 
  22.    File target = new File(COPY_FILE_TO); 
  23.    CopyFiles test = new CopyFiles(); 
  24.    test.copyWithChannels(source, target, false); 
  25.    //test.copyWithStreams(source, target, false); 
  26.    log("Done."); 
  27.  } 
  28.  
  29.  /** This may fail for VERY large files. */ 
  30.  private void copyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend) { 
  31.    log("Copying files with channels."); 
  32.    ensureTargetDirectoryExists(aTargetFile.getParentFile()); 
  33.    FileChannel inChannel = null
  34.    FileChannel outChannel = null
  35.    FileInputStream inStream = null
  36.    FileOutputStream outStream = null
  37.    try
  38.      try { 
  39.        inStream = new FileInputStream(aSourceFile); 
  40.        inChannel = inStream.getChannel(); 
  41.        outStream = new  FileOutputStream(aTargetFile, aAppend);         
  42.        outChannel = outStream.getChannel(); 
  43.        long bytesTransferred = 0
  44.        //defensive loop - there's usually only a single iteration : 
  45.        while(bytesTransferred < inChannel.size()){ 
  46.          bytesTransferred += inChannel.transferTo(0, inChannel.size(), outChannel); 
  47.        } 
  48.      } 
  49.      finally { 
  50.        //being defensive about closing all channels and streams  
  51.        if (inChannel != null) inChannel.close(); 
  52.        if (outChannel != null) outChannel.close(); 
  53.        if (inStream != null) inStream.close(); 
  54.        if (outStream != null) outStream.close(); 
  55.      } 
  56.    } 
  57.    catch (FileNotFoundException ex){ 
  58.      log("File not found: " + ex); 
  59.    } 
  60.    catch (IOException ex){ 
  61.      log(ex); 
  62.    } 
  63.  } 
  64.   
  65.  private void copyWithStreams(File aSourceFile, File aTargetFile, boolean aAppend) { 
  66.    log("Copying files with streams."); 
  67.    ensureTargetDirectoryExists(aTargetFile.getParentFile()); 
  68.    InputStream inStream = null
  69.    OutputStream outStream = null
  70.    try
  71.      try { 
  72.        byte[] bucket = new byte[32*1024]; 
  73.        inStream = new BufferedInputStream(new FileInputStream(aSourceFile)); 
  74.        outStream = new BufferedOutputStream(new FileOutputStream(aTargetFile, aAppend)); 
  75.        int bytesRead = 0
  76.        while(bytesRead != -1){ 
  77.          bytesRead = inStream.read(bucket); //-1, 0, or more 
  78.          if(bytesRead > 0){ 
  79.            outStream.write(bucket, 0, bytesRead); 
  80.          } 
  81.        } 
  82.      } 
  83.      finally { 
  84.        if (inStream != null) inStream.close(); 
  85.        if (outStream != null) outStream.close(); 
  86.      } 
  87.    } 
  88.    catch (FileNotFoundException ex){ 
  89.      log("File not found: " + ex); 
  90.    } 
  91.    catch (IOException ex){ 
  92.      log(ex); 
  93.    } 
  94.  } 
  95.   
  96.  private void ensureTargetDirectoryExists(File aTargetDir){ 
  97.    if(!aTargetDir.exists()){ 
  98.      aTargetDir.mkdirs(); 
  99.    } 
  100.  } 
  101.   
  102.  private static void log(Object aThing){ 
  103.    System.out.println(String.valueOf(aThing)); 
  104.  } 
  105. }