当前位置:网站首页>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();
}
}
}
}
}
边栏推荐
- 为什么sys_class 里显示的很多表的 RELTABLESPACE 值为 0 ?
- 今天是元宵节~~
- pytorch余弦退火学习率CosineAnnealingLR的使用
- PAT Class B-B1019 Digital Black Hole (20)
- seata源码解析:TM RM 客户端的初始化过程
- 2.4G无线收发模块的应用
- 无题十三
- 正则表达式replaceFirst()方法具有什么功能呢?
- Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
- Advanced usage of C language
猜你喜欢

Pytorch深度学习快速入门教程 -- 土堆教程笔记(三)

偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的

Assembly language (8) x86 inline assembly

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

基于 Kubernetes 的微服务项目整体设计与实现

Example of Noise Calculation for Amplifier OPA855

ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...

轩辕实验室丨欧盟EVITA项目预研 第一章(四)

dotnet OpenXML parsing PPT charts Getting started with area charts

Seata source code analysis: initialization process of TM RM client
随机推荐
开源一夏|OpenHarmony如何查询设备类型(eTS)
【LeetCode】623. Add a row to the binary tree
express hot-reload
无题三
PAT Grade B-B1020 Mooncake(25)
IO流篇 -- 基于io流实现文件夹拷贝(拷贝子文件夹及子文件夹内文件)满满的干货
15.1.1、md—md的基础语法,快速的写文本备忘录
请问大佬们 ,使用 Flink SQL CDC 是不是做不到两个数据库的实时同步啊
ffmpeg drawtext add text watermark
链表中的数字相加----链表专题
express hot-reload
2022.8.3
PAT Level B - B1021 Single Digit Statistics (15)
MySQL使用聚合函数可以不搭配GROUP BY分组吗?
Redis源码解析:Redis Cluster
Hbuilder 学习使用中的一些记录
PAT乙级-B1020 月饼(25)
2022.8.3
egg框架使用(一)
There is only one switch, how to realize the nqa of master-slave automatic switching