java將一個目錄下的所有數(shù)據(jù)復制到另一個目錄下
更新時間:2020年11月30日 17:10:56 作者:wangtianze
這篇文章主要為大家詳細介紹了java將一個目錄下的所有數(shù)據(jù)復制到另一個目錄下,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java將一個目錄下的所有數(shù)據(jù)復制到另一個目錄下的具體代碼,供大家參考,具體內容如下
/* 將"C:\\JavaProducts\\Source"下的所有數(shù)據(jù)復制到"C:\\Target"下 */ import java.io.*; public class JavaCopyDemo{ final static String SOURCESTRING = "C:\\JavaProducts\\Source"; final static String TARGETSTRING = "C:\\Target"; public static void main(String[] args){ if(!(new File(SOURCESTRING)).exists()){ System.out.println("源文件" + SOURCESTRING + "不存在,無法復制!"); return; }else if((new File(TARGETSTRING)).exists()){ System.out.println("目標文件" + TARGETSTRING + "已經存在,無法復制!"); return; }else{ if((new File(SOURCESTRING)).isFile()){ copyFile(new File(SOURCESTRING),new File(TARGETSTRING)); }else if((new File(SOURCESTRING)).isDirectory()){ copyDirectory(SOURCESTRING,TARGETSTRING); } } } private static void copyFile(File sourceFile,File targetFile){ if(!sourceFile.canRead()){ System.out.println("源文件" + sourceFile.getAbsolutePath() + "不可讀,無法復制!"); return; }else{ System.out.println("開始復制文件" + sourceFile.getAbsolutePath() + "到" + targetFile.getAbsolutePath()); FileInputStream fis = null; BufferedInputStream bis = null; FileOutputStream fos = null; BufferedOutputStream bos = null; try{ fis = new FileInputStream(sourceFile); bis = new BufferedInputStream(fis); fos = new FileOutputStream(targetFile); bos = new BufferedOutputStream(fos); int len = 0; while((len = bis.read()) != -1){ bos.write(len); } bos.flush(); }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }finally{ try{ if(fis != null){ fis.close(); } if(bis != null){ bis.close(); } if(fos != null){ fos.close(); } if(bos != null){ bos.close(); } System.out.println("文件" + sourceFile.getAbsolutePath() + "復制到" + targetFile.getAbsolutePath() + "完成"); }catch(IOException e){ e.printStackTrace(); } } } } private static void copyDirectory(String sourcePathString,String targetPathString){ if(!new File(sourcePathString).canRead()){ System.out.println("源文件夾" + sourcePathString + "不可讀,無法復制!"); return; }else{ (new File(targetPathString)).mkdirs(); System.out.println("開始復制文件夾" + sourcePathString + "到" + targetPathString); File[] files = new File(sourcePathString).listFiles(); for(int i = 0; i < files.length; i++){ if(files[i].isFile()){ copyFile(new File(sourcePathString + File.separator + files[i].getName()),new File(targetPathString + File.separator + files[i].getName())); }else if(files[i].isDirectory()){ copyDirectory(sourcePathString + File.separator + files[i].getName(),targetPathString + File.separator + files[i].getName()); } } System.out.println("復制文件夾" + sourcePathString + "到" + targetPathString + "結束"); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
詳解 Java Maximum redirects (100) exceeded
這篇文章主要介紹了詳解 Java Maximum redirects (100) exceeded的相關資料,需要的朋友可以參考下2017-05-05Java 如何將網(wǎng)絡資源url轉化為File文件
這篇文章主要介紹了Java 如何將網(wǎng)絡資源url轉化為File文件的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Spring動態(tài)管理定時任務之ThreadPoolTaskScheduler解讀
這篇文章主要介紹了Spring動態(tài)管理定時任務之ThreadPoolTaskScheduler解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12