java實現(xiàn)系統(tǒng)多級文件夾復制
更新時間:2021年08月10日 15:16:20 作者:Jae_Du
這篇文章主要為大家詳細介紹了java實現(xiàn)系統(tǒng)多級文件夾復制,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現(xiàn)系統(tǒng)多級文件夾復制的具體代碼,供大家參考,具體內容如下
package com.jae; import java.io.*; //復制文件夾內的內容,包含多級文件夾 public class Test2 { public static void main(String[] args) throws Exception { //原文件夾地址 File resPath = new File("E:\\Java\\分享"); File destPath = new File("E:\\"); //method(resPath, destPath); copy(resPath, destPath); } public static void copy(File src,File dest) throws Exception { File newDir = new File(dest,src.getName()); newDir.mkdir(); File[] subFiles = src.listFiles(); for (File subFile : subFiles) { if(subFile.isFile()){ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile)); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( new File(newDir,subFile.getName()))); int b; byte[] bytes = new byte[1024 * 8]; while((b = bis.read(bytes)) != -1){ bos.write(bytes,0,b); } bis.close(); bos.close(); }else{ copy(subFile,newDir);//遞歸調用 } } } public static void method(File dir, File destPath) throws IOException { //獲取源路徑的文件 File[] files = dir.listFiles(); //根目錄文件夾名 String name1 = dir.getName(); //遍歷源路徑的文件 for (File file : files) { if (file.isFile()) { StringBuilder sb = new StringBuilder(destPath.getAbsolutePath()); sb.append("\\").append(file.getName()); //獲取盤符后面的路徑和文件名 //String s = absolutePath.split(":")[1]; //創(chuàng)建輸入流,封裝獲取到的文件的絕對路徑 FileInputStream fis = new FileInputStream(file.getAbsolutePath()); //目標路徑,定義目標路徑的盤符,和要復制的文件路徑和文件名 //FileOutputStream fos = new FileOutputStream("F:" + s); FileOutputStream fos = new FileOutputStream(sb.toString()); //復制文件操作 int len; byte[] bytes = new byte[1024 * 8]; while ((len = fis.read(bytes)) != -1) { fos.write(bytes, 0, len); fos.flush(); } fos.close(); fis.close(); } else { StringBuilder sb = new StringBuilder(destPath.getAbsolutePath()); sb.append("\\").append(name1).append("\\").append(file.getName()); /*String name1 = file.getName(); //獲取文件夾的路徑 String name = file.getPath(); //獲取盤符:后的文件夾路徑 String s = name.split(":")[1];*/ //創(chuàng)建文件夾路徑 //File file1 = new File("F:" + name1); File file1 = new File(sb.toString()); file1.mkdirs(); //System.out.println(name); method(file, file1); } } } }
再為大家補充一段代碼:Java遞歸復制多級目錄及文件,感謝原作者的分享
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* * 需求:復制多級文件夾 * * 數(shù)據(jù)源:D:\\1 * 目的地:F:\\新建文件夾 * * 分析: * A:封裝數(shù)據(jù)源File * B:封裝目的地File * C:判斷該File是文件夾還是文件 * a:是文件夾 * 就在目的地目錄下創(chuàng)建該文件夾 * 獲取該File對象下的所有文件或者文件夾File對象 * 遍歷得到每一個File對象 * 回到C * b:是文件 * 就復制(字節(jié)流) */ public class C1 { public static void main(String[] args) throws IOException { File srcFolder = new File("D:\\1"); File dstFolder = new File("F:\\新建文件夾"); judge(srcFolder,dstFolder); } private static void judge(File srcFolder,File dstFolder) throws IOException { if(srcFolder.isDirectory()){ File newFolder = new File(dstFolder,srcFolder.getName()); newFolder.mkdir(); File[] fileArr = srcFolder.listFiles(); for(File f:fileArr){ judge(f, newFolder); } }else{ File newFile = new File(dstFolder,srcFolder.getName()); // System.out.println(newFile); copyFile(srcFolder,newFile); } } private static void copyFile(File srcFolder, File newFile) throws IOException { // TODO Auto-generated method stub BufferedInputStream bis = new BufferedInputStream(new FileInputStream( srcFolder)); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(newFile)); byte[] bys = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); } bos.close(); bis.close(); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java mysql數(shù)據(jù)庫并進行內容查詢實例代碼
這篇文章主要介紹了Java mysql數(shù)據(jù)庫并進行內容查詢實例代碼的相關資料,需要的朋友可以參考下2016-11-11SpringBoot整合Redis實現(xiàn)訪問量統(tǒng)計的示例代碼
本文主要介紹了SpringBoot整合Redis實現(xiàn)訪問量統(tǒng)計的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解步驟詳解
這篇文章主要介紹了Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解步驟,本文分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09SpringBoot如何配置文件properties和yml
這篇文章主要介紹了SpringBoot如何配置文件properties和yml問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08