当前位置:网站首页>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();
}
}
}
}
}
边栏推荐
- Seata source code analysis: initialization process of TM RM client
- dotnet OpenXML 解析 PPT 图表 面积图入门
- 【Excel实战】--图表联动demo_001
- MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
- flink cdc支持从oracle dg库同步吗
- 2022.8.3
- 干货!生成模型的评价与诊断
- IO流篇 -- 基于io流实现文件夹拷贝(拷贝子文件夹及子文件夹内文件)满满的干货
- Is there a problem with writing this?How to synchronize data in sql-client
- leetcode: 529. Minesweeper Game
猜你喜欢
随机推荐
轩辕实验室丨欧盟EVITA项目预研 第一章(四)
(转)[Json]net.sf.json 和org.json 的差别及用法
Undefined symbols for architecture arm64解决方案
my journal link
七夕浪漫约会不加班,RPA机器人帮你搞定工作
What is the function of the regular expression replaceAll() method?
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)
新白娘子传奇系列
科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系
EU | Horizon 2020 ENSEMBLE: D2.13 SOTIF Safety Concept (Part 2)
正则表达式replaceAll()方法具有什么功能呢?
Creo 9.0 基准特征:基准平面
tensorflow.keras无法引入layers
How to realize the short press and long press detection of the button?
dotnet OpenXML 解析 PPT 图表 面积图入门
leetcode points to Offer 10- I. Fibonacci sequence
js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
The Seven Weapons of Programmers
无题五
为什么sys_class 里显示的很多表的 RELTABLESPACE 值为 0 ?








