java實(shí)現(xiàn)系統(tǒng)多級(jí)文件夾復(fù)制
本文實(shí)例為大家分享了java實(shí)現(xiàn)系統(tǒng)多級(jí)文件夾復(fù)制的具體代碼,供大家參考,具體內(nèi)容如下
package com.jae; import java.io.*; //復(fù)制文件夾內(nèi)的內(nèi)容,包含多級(jí)文件夾 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);//遞歸調(diào)用 } } } 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)建輸入流,封裝獲取到的文件的絕對(duì)路徑 FileInputStream fis = new FileInputStream(file.getAbsolutePath()); //目標(biāo)路徑,定義目標(biāo)路徑的盤符,和要復(fù)制的文件路徑和文件名 //FileOutputStream fos = new FileOutputStream("F:" + s); FileOutputStream fos = new FileOutputStream(sb.toString()); //復(fù)制文件操作 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); } } } }
再為大家補(bǔ)充一段代碼:Java遞歸復(fù)制多級(jí)目錄及文件,感謝原作者的分享
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* * 需求:復(fù)制多級(jí)文件夾 * * 數(shù)據(jù)源:D:\\1 * 目的地:F:\\新建文件夾 * * 分析: * A:封裝數(shù)據(jù)源File * B:封裝目的地File * C:判斷該File是文件夾還是文件 * a:是文件夾 * 就在目的地目錄下創(chuàng)建該文件夾 * 獲取該File對(duì)象下的所有文件或者文件夾File對(duì)象 * 遍歷得到每一個(gè)File對(duì)象 * 回到C * b:是文件 * 就復(fù)制(字節(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(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java mysql數(shù)據(jù)庫并進(jìn)行內(nèi)容查詢實(shí)例代碼
這篇文章主要介紹了Java mysql數(shù)據(jù)庫并進(jìn)行內(nèi)容查詢實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11SpringBoot整合Redis實(shí)現(xiàn)訪問量統(tǒng)計(jì)的示例代碼
本文主要介紹了SpringBoot整合Redis實(shí)現(xiàn)訪問量統(tǒng)計(jì)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Spring Cache擴(kuò)展功能實(shí)現(xiàn)過程解析
這篇文章主要介紹了Spring Cache擴(kuò)展功能實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02java操作excel導(dǎo)入導(dǎo)出的3種方式
項(xiàng)目需要,要實(shí)現(xiàn)一個(gè)導(dǎo)入導(dǎo)出excel的功能,于是任務(wù)驅(qū)動(dòng)著我學(xué)習(xí)到了POI、easypoi和easyexcel這3個(gè)java操作Excel的工具,下面這篇文章主要給大家介紹了關(guān)于java操作excel導(dǎo)入導(dǎo)出的3種方式,需要的朋友可以參考下2023-05-05Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解步驟詳解
這篇文章主要介紹了Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解步驟,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09SpringBoot如何配置文件properties和yml
這篇文章主要介紹了SpringBoot如何配置文件properties和yml問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Java 使用Axis調(diào)用WebService的示例代碼
這篇文章主要介紹了Java 使用Axis調(diào)用WebService的示例代碼,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-09-09