当前位置:网站首页>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();
}
}
}
}
}
边栏推荐
- mySQL数据库初始化失败,有谁可以指导一下吗
- 嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)
- The difference between beautiful MM and ordinary MM
- Dry goods!Generative Model Evaluation and Diagnosis
- selectPage 动态改变参数方法
- eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
- 2022.8.3
- 随时牵手 不要随意分手[转帖]
- 百行代码发射红心,程序员何愁命不中女朋友!
- Luogu P4588: [TJOI2018]数学计算
猜你喜欢
ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
动态内存开辟(C语言)
Weekly Report 2022-8-4
Undefined symbols for architecture arm64解决方案
基因数据平台
2.4G无线收发模块的应用
mySQL数据库初始化失败,有谁可以指导一下吗
营销建议 | 您有一份八月营销月历待查收! 建议收藏 !
Embedded practice ---- based on RT1170 transplant memtester to do SDRAM test (25)
Creo 9.0 基准特征:基准坐标系
随机推荐
How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
IT研发/开发流程规范效能的思考总结
seata源码解析:事务状态及全局锁的存储
无题十
C语言-数组
Is there a problem with writing this?How to synchronize data in sql-client
Two-table query average grouping in sql server
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Clothing Image Classification (Day 3)
JS syntax usage
微信小程序请求封装
如何实现按键的短按、长按检测?
明天去订票,准备回家咯~~
16种香饭做法全攻略
PAT乙级-B1021 个位数统计(15)
NC20164 :最大数MAXNUMBER [线段树]
2022-08-01 回顾基础二叉树以及操作
DTcloud 装饰器
工程制图直线投影练习
基因数据平台
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)