1 package com.filetest; 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.io.IOException; 9 import java.util.Scanner; 10 11 public class copyfile { 12 13 public static void main(String args[]) throws IOException{ 14 15 copy(); //調用復制函數 16 System.out.println("復制完成!"); //提示復制完成 17 } 18 19 private static void copy() throws IOException { 20 21 System.out.println("輸入你要復制的文件路徑及名稱:"); 22 Scanner scanner=new Scanner(System.in); 23 String oldpath=scanner.next(); //接收原文件夾路徑及名稱 24 25 File ofile=new File(oldpath); 26 if((!ofile.exists()||!ofile.isDirectory())){ //判斷源文件夾路徑是否存在 27 28 if(oldpath.equals("end")) //路徑不存在則進入判斷內,如果輸入的不是end則遞歸調用重新輸入 29 { 30 System.out.println("程序結束,感謝使用!"); 31 System.exit(-1); 32 } 33 else 34 { 35 System.out.println("輸入的源文件夾路徑不存在,請重新輸入!(輸入end退出程序)"); 36 copy(); 37 } 38 39 } 40 41 System.out.println("輸入你要復制該文件到哪個路徑:"); 42 String newpath=scanner.next(); //接收目標文件夾路徑及名稱 43 44 File nfile=new File(newpath); 45 if(!nfile.isAbsolute()){ //判斷目標文件夾路徑是否為目錄 46 if(newpath.equals("end")) //路徑不存在則進入判斷內,如果輸入的不是end則遞歸調用重新輸入 47 { 48 System.out.println("程序結束,感謝使用!"); 49 System.exit(-1); 50 } 51 else 52 { 53 System.out.println("輸入的目標文件夾目錄格式不正確,請重新輸入!(輸入end退出程序)"); 54 copy(); 55 } 56 57 } 58 59 //截取源文件夾路徑最后的名字 60 String laststr = oldpath.substring(oldpath.lastIndexOf("/"), oldpath.length()); 61 copyDirectiory(oldpath,newpath+"/"+laststr); //將原路徑文件夾名稱和目標路徑文件夾名稱傳遞給復制文件夾函數 62 63 64 } 65 66 67 //用緩沖流復制文件函數 68 public static void copyFile(File sourceFile,File targetFile) 69 throws IOException{ 70 // 新建文件輸入流並對它進行緩沖 71 FileInputStream input = new FileInputStream(sourceFile); 72 BufferedInputStream inBuff=new BufferedInputStream(input); 73 74 // 新建文件輸出流並對它進行緩沖 75 FileOutputStream output = new FileOutputStream(targetFile); 76 BufferedOutputStream outBuff=new BufferedOutputStream(output); 77 78 int len; 79 while ((len =inBuff.read()) != -1) 80 { 81 outBuff.write(len); 82 } 83 // 刷新此緩沖的輸出流 84 outBuff.flush(); 85 86 //關閉流 87 inBuff.close(); 88 outBuff.close(); 89 output.close(); 90 input.close(); 91 } 92 93 94 // 復制文件夾函數 95 public static void copyDirectiory(String sourceDir, String targetDir) 96 throws IOException { 97 98 File aimfile=new File(targetDir); 99 if(!(aimfile).exists()){ //查看目錄是否存在,不存在則新建 100 aimfile.mkdirs(); 101 } 102 103 if(sourceDir.equals(targetDir)){ //如果文件路徑及文件名相同則覆蓋 104 System.out .println("文件已存在,是否覆蓋(N退出/任意鍵繼續)?"); 105 Scanner scanner=new Scanner(System.in); 106 String NY=scanner.next(); 107 if(NY.equalsIgnoreCase("n")){ //如果不想覆蓋 可退出程序 108 System.out.println("程序結束,感謝使用!"); 109 System.exit(-1); 110 } 111 112 } 113 114 // 獲取源文件夾下的文件或目錄 115 File oldfile=new File(sourceDir); 116 File[] file=oldfile.listFiles(); 117 118 for(int i=0;i<file.length;i++) 119 { 120 121 if (file[i].isFile()) //如果是文件,傳遞給copyFile()函數進行復制 122 { 123 //目標文件 124 File aim=new File(targetDir); 125 File targetFile=new File(aim.getAbsolutePath()+"/"+file[i].getName()); 126 copyFile(file[i],targetFile); 127 } 128 if (file[i].isDirectory()) //如果是文件夾,則遞歸調用 129 { 130 // 要遞歸復制的源文件夾 131 String soursefiles=sourceDir + "/" + file[i].getName(); 132 133 // 要遞歸復制的目標文件夾 134 String aimfiles=targetDir + "/"+ file[i].getName(); 135 136 copyDirectiory(soursefiles, aimfiles); 137 } 138 } 139 } 140 }
其實在復制單個文件的時候可以優化一下 用FileChannel比緩沖復制效率高三分之一。僅供大家參考。