当前位置:网站首页>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();
}
}
}
}
}
边栏推荐
- What is CRM Decision Analysis Management?
- Keil升级到AC6后,到底有哪些变化?
- 明天去订票,准备回家咯~~
- 【zeno】为zeno增加子模块/新节点的最小化的例子
- 21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)
- selectPage 动态改变参数方法
- 营销建议 | 您有一份八月营销月历待查收! 建议收藏 !
- 5.部署web项目到云服务器
- Happens-before rules for threads
- 这样写有问题吗?怎么在sql-client 是可以做到数据的同步的
猜你喜欢
随机推荐
Xcode 12 ld: symbol(s) not found for architecture armv64
交换机端口的三种类型详解与hybrid端口实验
After Keil upgrades to AC6, what changes?
2022/8/4 考试总结
CCVR基于分类器校准缓解异构联邦学习
无题二
Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need
2022-08-01 回顾基础二叉树以及操作
Weekly Report 2022-8-4
无题三
Voice conversion相关语音数据集综合汇总
PAT Grade B-B1020 Mooncake(25)
今天是元宵节~~
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
PAT乙级-B1019 数字黑洞(20)
Which big guy has the 11G GI and ojvm patches in April or January 2020, please help?
How to realize the short press and long press detection of the button?
19.服务器端会话技术Session
tensorflow.keras无法引入layers
Xcode10的打包方式distribute app和启动项目报错以及Xcode 打包本地ipa包安装到手机上








