当前位置:网站首页>IO流篇 -- 基于io流实现文件夹拷贝(拷贝子文件夹及子文件夹内文件)满满的干货
IO流篇 -- 基于io流实现文件夹拷贝(拷贝子文件夹及子文件夹内文件)满满的干货
2022-08-05 09:14:00 【InfoQ】
public class FileCopy {
public static void main(String[] args) {
//源文件
String path = "C:/Users/Desktop/0611";
//目标地址
String path2 = "C:/Users/Desktop/0611(复制版)";
//将目标路径放入File类中
File f2 = new File(path2);
//检测文件路径是否存在,若不存在,则创建文件夹,防止文件未找到异常导致程序崩溃
if (!f2.exists()) {
//创建文件夹
f2.mkdirs();
}
//调用方法,传入实参,开始拷贝文件夹
copyFile(path, path2);
}
/**
* 拷贝文件,利用递归拷贝子文件夹以及文件
* @param path1 源文件路径
* @param path2 目标文件路径
*/
public static void copyFile(String path1, String path2) {
File file = new File(path1);
File[] files = file.listFiles();
for (File file2 : files) {
if (file2.isDirectory()) {
String newPath = path2 + File.separator + file2.getName();
File f2 = new File(newPath);
System.out.println(f2.getAbsolutePath());
f2.mkdirs();
System.out.println("创建成功~");
copyFile(file2.getAbsolutePath(), newPath);
}
if (file2.isFile()) {
try {
InputStream is = new FileInputStream(file2.getAbsolutePath());
int num = is.available();
byte[] bs = new byte[num];
OutputStream os = new FileOutputStream(path2 + File.separator + file2.getName());
int realLen = is.read(bs, 0, bs.length);
os.write(bs, 0, realLen);
System.out.println("写入成功~");
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
边栏推荐
- CCVR eases heterogeneous federated learning based on classifier calibration
- 16 kinds of fragrant rice recipes
- 汇编语言(8)x86内联汇编
- Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
- 明天去订票,准备回家咯~~
- 2022.8.3
- 只有一台交换机,如何实现主从自动切换之nqa
- 16种香饭做法全攻略
- PAT乙级-B1019 数字黑洞(20)
- PAT Class B-B1019 Digital Black Hole (20)
猜你喜欢

手把手教你纯c实现异常捕获try-catch组件

让程序员崩溃的N个瞬间(非程序员误入)

Weekly Report 2022-8-4

上海控安技术成果入选市经信委《2021年上海市网络安全产业创新攻关成果目录》

seata源码解析:事务状态及全局锁的存储

js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)

链表中的数字相加----链表专题

Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g

CCVR基于分类器校准缓解异构联邦学习

Hundred lines of code launch red hearts, why programmers lose their girlfriends!
随机推荐
Dynamic memory development (C language)
Luogu P4588: [TJOI2018]数学计算
画法几何及工程制图考试卷A卷
Hundred lines of code launch red hearts, why programmers lose their girlfriends!
并发之CAS
express hot-reload
无题十
sphinx匹配指定字段
openpyxl操作Excel文件
树状数组模版+例题
全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了
HStreamDB Newsletter 2022-07|分区模型优化、数据集成框架进一步完善
Creo 9.0 基准特征:基准点
Luogu P1966: [NOIP2013 提高组] 火柴排队 [树状数组+逆序对]
pytorch余弦退火学习率CosineAnnealingLR的使用
Xcode10的打包方式distribute app和启动项目报错以及Xcode 打包本地ipa包安装到手机上
【ASM】字节码操作 方法的初始化 Frame
按钮上显示值的轮流切换
CVPR 2022 | 将X光图片用于垃圾分割,港中大(深圳)探索大规模智能垃圾分类
营销建议 | 您有一份八月营销月历待查收! 建议收藏 !