当前位置:网站首页>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();
}
}
}
}
}
边栏推荐
猜你喜欢
Example of Noise Calculation for Amplifier OPA855
IDEA执行Test操作导致数据插入时出现了重复数据
蚁剑webshell动态加密连接分析与实践
Custom filters and interceptors implement ThreadLocal thread closure
egg框架使用(二)
Which big guy has the 11G GI and ojvm patches in April or January 2020, please help?
Oracle temporary table space role
leetcode: 529. Minesweeper Game
2022.8.3
dotnet OpenXML parsing PPT charts Getting started with area charts
随机推荐
Overall design and implementation of Kubernetes-based microservice project
leetcode refers to Offer 10- II. Frog jumping steps
【ASM】字节码操作 方法的初始化 Frame
只有一台交换机,如何实现主从自动切换之nqa
手写柯里化 - toString 理解
IDEA执行Test操作导致数据插入时出现了重复数据
程序员的七种武器
科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系
2022/8/4 考试总结
Advanced usage of C language
EU | Horizon 2020 ENSEMBLE: D2.13 SOTIF Safety Concept (Part 2)
无题六
2022-08-01 回顾基础二叉树以及操作
Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
Undefined symbols for architecture arm64解决方案
为什么我推荐使用智能化async?
深度学习21天——卷积神经网络(CNN):天气识别(第5天)
2022.8.3
韦东山 数码相框 项目学习(六)tslib的移植
PAT Grade B-B1020 Mooncake(25)