標籤:file類
輸入需要複製的目標檔案夾
輸入需要複製到的目的檔案夾
public class CopyDir {static Scanner in = new Scanner(System.in);static File dir=null;public static void main(String[] args) {System.out.println("請輸入需要複製的目標檔案夾的路徑:");String need = in.nextLine();dir = new File(need);//dir = new File("F:\\0123");String fordir = dir.getPath();System.out.println("請輸入複製到目的檔案夾的路徑:");String destDir = in.nextLine();File dest = new File(destDir);//把目的檔案夾封裝成檔案對象 主要是判斷該目的檔案夾下是否 含有目標檔案夾//目標檔案夾 複製到該檔案夾下//File dest = new File("F:");//\\MyServer//將目標檔案夾的所有檔案以及檔案夾 複製到該檔案夾下 該檔案夾的名字肯能和目標檔案夾同名destDir = dest.getPath()+"\\"+dir.getName();if(!dest.isDirectory()){System.out.println("請輸入一個檔案夾的路徑");return;}//判斷該目的檔案夾下是否 含有目標檔案夾else{//主要是想練習下 檔案過濾 才寫的以下代碼 判斷該目的檔案夾下是否 含有目標檔案夾////如果dest為 檔案而不是檔案夾的話 listFiles方法會拋出NullPointerException異常//File[] files = dest.listFiles(new FilenameFilter(){//public boolean accept(File dir, String name) {//System.out.println(name);//dir就是dest 那麼name就是dest下所有檔案的名字包括檔案夾和檔案//return name.equals(CopyDir.this.dir.getName());//}});//System.out.println(files.length);////長度等於1的時候說明複製到的目標檔案夾下 有同名檔案夾 也就是說 被複製的檔案夾在複製到的檔案夾內 此時 建立檔案夾副本//if(files.length==1){//destDir = dest.getPath()+"\\"+dir.getName()+"-副本";//判斷該目的檔案夾下是否 含有目標檔案夾 通過檔案是否存在來判斷File toDir = new File(destDir);if (toDir.exists()) {destDir = dest.getPath()+"\\"+dir.getName()+"-副本";toDir = new File(destDir);}int count = 0;while (toDir.exists()) {count++;if(!destDir.contains("-副本("+(count-1)+")"))destDir = dest.getPath()+"\\"+dir.getName()+"-副本(1)";elsedestDir = destDir.replace("-副本("+(count-1)+")", "-副本("+count+")");toDir = new File(destDir);}toDir.mkdir();}if (dir.isDirectory()){System.out.println("開始複製。。。");copyDir(dir,fordir,destDir);}else{System.out.println("您輸入的不是檔案夾!");return;}System.out.println("目標檔案夾:"+dir.getName()+"->複製到目的檔案夾:"+destDir+",,複製成功");}public static void copyDir(File dir,String fordir,String destDir) {File[] files = dir.listFiles();for (File file : files) {//注意 要先建立該檔案夾對應的目的的新同名檔案夾 然後遞迴if(file.isDirectory()){//將檔案夾內的檔案複製到另一個檔案夾中 在複製檔案前 需要先建立存放檔案的檔案夾File toDir = new File(file.getPath().replace(fordir, destDir));toDir.mkdir();//先建立 檔案夾 再遞迴copyDir(file,fordir,destDir);}else{//複製的新檔案File tofile = new File(file.getPath().replace(fordir, destDir));try {tofile.createNewFile();FileInputStream fis = new FileInputStream(file);FileOutputStream fos = new FileOutputStream(tofile);byte[] buf = new byte[1024];int len = 0;while ((len = fis.read(buf))!=-1) {fos.write(buf,0,len);}fis.close();fos.close();} catch (Exception e) {e.printStackTrace();}}}}}
/*複製檔案夾從原檔案夾中讀取檔案 賦值到檔案夾副本中 實際上就是複製檔案到 建立的檔案夾副本中1,從原檔案夾中讀取資料fs[i].isFile() fs[i]是 檔案的時候 直接複製2,fs[i]是檔案夾的時候 首先 建立子檔案夾副本 然後將原子檔案夾內的檔案複製到檔案夾副本中子檔案夾副本在上一級檔案夾副本中 定義一個字串儲存 檔案路徑*/class copyDir{String s = "null";//最外層 目錄路徑String path = "null";//定義一個字串儲存 檔案路徑public void readDir(File f,int lev) throws Exception//形參f代表的是 原檔案夾{lev++;if(lev == 1){s = f.getName();System.out.println(s);path = f.getPath().replaceAll(s,s+"副本");System.out.println(path);File dir = new File(path);System.out.println(dir.mkdir());}//原檔案中的資料File[] fs = f.listFiles();for (int i=0 ;i<fs.length ;i++ ){if (fs[i].isFile()){path = fs[i].getParent().replaceAll(s,s+"副本");File fc = new File(path,fs[i].getName());//檔案建立之前 一定要保證路徑存(檔案所在的檔案夾)在 否則拋異常fc.createNewFile(); //將檔案 複製到新檔案中BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fs[i]));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fc));byte[] buf = new byte[1024];int len=0;while ((len=bis.read(buf,0,1024))!=-1){bos.write(buf,0,len);}bos.close();bis.close();}//是檔案夾的話 建立檔案夾副本if (fs[i].isDirectory()){path = fs[i].getPath().replaceAll(s,s+"副本");;File dir = new File(path);//建立檔案夾dir.mkdir();readDir(fs[i],lev);}}}public static void main(String[] args) throws Exception{File f = new File("F:\\0123");Long start = System.currentTimeMillis();new copyDir().readDir(f,0);Long over = System.currentTimeMillis();System.out.println("檔案夾複製成功,耗時:"+(over-start));}}
本文出自 “要麼拚命,要麼滾回去!” 部落格,請務必保留此出處http://jiangzuun2014.blog.51cto.com/8732469/1532185