当前位置:网站首页>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();
}
}
}
}
}
边栏推荐
- Creo 9.0 基准特征:基准轴
- 无题六
- MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
- 我的杂记链接
- sql server中 两表查询 平均数 分组
- js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
- egg framework
- PAT乙级-B1020 月饼(25)
- Assembly language (8) x86 inline assembly
- 明天去订票,准备回家咯~~
猜你喜欢

Weekly Report 2022-8-4

使用稀疏 4D 卷积对 3D LiDAR 数据中的运动对象进行后退分割(IROS 2022)

【LeetCode】623. Add a row to the binary tree

接口全周期的生产力利器Apifox

21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)

DTcloud 装饰器

工程制图直线投影练习

pytorch余弦退火学习率CosineAnnealingLR的使用

如何实现按键的短按、长按检测?

Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
随机推荐
DPU — 功能特性 — 安全系统的硬件卸载
seata源码解析:事务状态及全局锁的存储
PAT Level B - B1021 Single Digit Statistics (15)
Xcode 12 ld: symbol(s) not found for architecture armv64
树状数组模版+例题
SQL语句查询字段内重复内容,并按重复次数加序号
DPU — 功能特性 — 管理系统的硬件卸载
thinkPHP5 realizes clicks (data increment/decrement)
在colab里怎样读取google drive数据
leetcode points to Offer 10- I. Fibonacci sequence
【ASM】字节码操作 方法的初始化 Frame
express hot-reload
Excuse me if you want to write data in mysql, with flink - connector - JDBC directly is ok, but I'm in the f
请问大佬们 ,使用 Flink SQL CDC 是不是做不到两个数据库的实时同步啊
Creo 9.0 基准特征:基准点
The difference between beautiful MM and ordinary MM
leetcode 剑指 Offer 10- II. 青蛙跳台阶问题
How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
Assembly language (8) x86 inline assembly
openpyxl to manipulate Excel files