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());
//獲取盤(pán)符后面的路徑和文件名
//String s = absolutePath.split(":")[1];
//創(chuàng)建輸入流,封裝獲取到的文件的絕對(duì)路徑
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
//目標(biāo)路徑,定義目標(biāo)路徑的盤(pán)符,和要復(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();
//獲取盤(pán)符:后的文件夾路徑
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)文章
Springboot 中使用 Aop代碼實(shí)戰(zhàn)教程
AOP的編程思想是把對(duì)類對(duì)象的橫切問(wèn)題點(diǎn),從業(yè)務(wù)邏輯中分離出來(lái),從而達(dá)到解耦的目的,增加代碼的復(fù)用性,提高開(kāi)發(fā)效率,這篇文章主要介紹了Springboot中使用Aop代碼實(shí)戰(zhàn)教程,需要的朋友可以參考下2023-07-07
解析Mybatis SqlSessionFactory初始化原理
本文主要介紹了Mybatis SqlSessionFactory初始化原理,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
java中volatile不能保證線程安全(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇java中volatile不能保證線程安全(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Java 如何將前端傳來(lái)的數(shù)字轉(zhuǎn)化為日期
這篇文章主要介紹了Java 如何將前端傳來(lái)的數(shù)字轉(zhuǎn)化為日期,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
如何在springboot中實(shí)現(xiàn)頁(yè)面的國(guó)際化
今天帶大家學(xué)習(xí)如何在springboot中實(shí)現(xiàn)頁(yè)面的國(guó)際化,文中有非常詳細(xì)的圖文解說(shuō)及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05
SpringBoot+Dubbo+Seata分布式事務(wù)實(shí)戰(zhàn)詳解
這篇文章主要介紹了SpringBoot+Dubbo+Seata分布式事務(wù)實(shí)戰(zhàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
java使用MulticastSocket實(shí)現(xiàn)基于廣播的多人聊天室
這篇文章主要為大家詳細(xì)介紹了java使用MulticastSocket實(shí)現(xiàn)基于廣播的多人聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01

