当前位置:网站首页>IO stream articles -- based on io stream to realize folder copy (copy subfolders and files in subfolders) full of dry goods
IO stream articles -- based on io stream to realize folder copy (copy subfolders and files in subfolders) full of dry goods
2022-08-05 09:32:00 【InfoQ】
public class FileCopy {
public static void main(String[] args) {
//Source file
String path = "C:/Users/Desktop/0611";
//Destination address
String path2 = "C:/Users/Desktop/0611(copy version)";
//Put the target path into the File class
File f2 = new File(path2);
//Detect the file pathWhether it exists, if not, create a folder to prevent the program from crashing due to a file not found exception
if (!f2.exists()) {
//Create a folder
f2.mkdirs();
}
//Call the method, pass in the actual parameters, and start copying the folder
copyFile(path, path2);
}
/**
* Copy files, copy subfolders and files recursively
* @param path1 Source file path
* @param path2 Destination file path
*/
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("created successfully~");
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("Write successful~");
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();
}
}
}
}
}
边栏推荐
猜你喜欢

Microservice Technology Stack

express hot-reload

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

Hundred lines of code launch red hearts, why programmers lose their girlfriends!

sql server中 两表查询 平均数 分组

eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑

mysql进阶(二十七)数据库索引原理

2022.8.3

微服务 技术栈

Example of Noise Calculation for Amplifier OPA855
随机推荐
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Clothing Image Classification (Day 3)
并发之CAS
C语言-数组
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
【ASM】字节码操作 方法的初始化 Frame
无题十四
CPU的亲缘性affinity
Imitation SBUS fixed with serial data conversion
Dry goods!Generative Model Evaluation and Diagnosis
What is the function of the regular expression replaceAll() method?
ffmpeg drawtext 添加文本水印
pytorch余弦退火学习率CosineAnnealingLR的使用
Weekly Report 2022-8-4
上海控安技术成果入选市经信委《2021年上海市网络安全产业创新攻关成果目录》
无题八
js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
PAT乙级-B1020 月饼(25)
正则表达式replaceAll()方法具有什么功能呢?
PAT Grade B-B1020 Mooncake(25)
mysql进阶(二十七)数据库索引原理