廣告

2015年3月11日 星期三

[java ] 4 way to copy file & 2 way to copy folder


package test_fb_api;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import org.apache.commons.io.FileUtils;

public class test4wayCopy {

 public static void main(String[] args) {
  
  System.out.println("copy file");
  String src = "C:/Users/1409035/Desktop/FTP_server_backup/test.txt";
  String des = "C:/Users/1409035/Desktop/FTP_server_backup/candelete/test1.txt";
  copyFileUsingApacheCommonsIO(new File(src), new File(des));
  
  des = "C:/Users/1409035/Desktop/FTP_server_backup/candelete/test2.txt";
  copyFileUsingFileStreams(new File(src), new File(des));
  
  des = "C:/Users/1409035/Desktop/FTP_server_backup/candelete/test3.txt";
  copyFileUsingFileChannels(new File(src), new File(des));
  
  des = "C:/Users/1409035/Desktop/FTP_server_backup/candelete/test4.txt";
  copyFileUsingFileChannels(new File(src), new File(des));
  
  
  System.out.println("done");
  src = "C:/Users/1409035/Desktop/上新版本計畫/eInvoice/";
  des = "C:/Users/1409035/Desktop/FTP_server_backup/candelete/";
  
  copyAllFolder(new File(src), new File(des));
  
  copyAllFolderRecursive(new File(src), new File(des));
  
  System.out.println("copy all folder");
  
 }

 public static void copyAllFolderRecursive(File src, File dest)
 {

  if (src.isDirectory()) {

   // if directory not exists, create it
   if (!dest.exists()) {
    dest.mkdir();
    System.out.println("Directory copied from " + src + "  to "
      + dest);
   }

   // list all the directory contents
   String files[] = src.list();

   for (String file : files) {
    // construct the src and dest file structure
    File srcFile = new File(src, file);
    File destFile = new File(dest, file);
    // recursive copy
    copyAllFolderRecursive(srcFile, destFile);
   }

  } else {
   copyFileUsingApacheCommonsIO(src.getAbsoluteFile(), dest.getAbsoluteFile());
   System.out.println("File copied from " + src + " to " + dest);
  }

 }
 
 
 private static void copyAllFolder(File source, File dest){
  
  try {
   FileUtils.copyDirectory(source, dest);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
 
 /*
  * cover destination file ,although destination file is exist 
  */
 private static void copyFileUsingApacheCommonsIO(File source, File dest)
 {
  try {
   FileUtils.copyFile(source, dest);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
  
 private static void copyFileUsingFileStreams(File source, File dest)
 {
  InputStream input = null;
  OutputStream output = null;
  try {

   input = new FileInputStream(source);
   output = new FileOutputStream(dest);
   byte[] buf = new byte[1024];
   int bytesRead;
   while ((bytesRead = input.read(buf)) > 0) {
    output.write(buf, 0, bytesRead);
   }
   input.close();
   output.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();

  } finally {

  }
 }
 
 /*
  * this one is fastest
  */
 private static void copyFileUsingFileChannels(File source, File dest) {
  FileChannel inputChannel = null;
  FileChannel outputChannel = null;

  try {
   inputChannel = new FileInputStream(source).getChannel();
   outputChannel = new FileOutputStream(dest).getChannel();
   outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
   inputChannel.close();
   outputChannel.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  finally {

  }
 }
 
 
 private static void copyFileUsingJava7Files(File source, File dest)
 {
  try {
   Files.copy(source.toPath(), dest.toPath());
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
}

沒有留言:

張貼留言