当前位置:网站首页>IO流文件复制
IO流文件复制
2022-08-06 05:24:00 【半世晨晓1128】
1.文件复制
package Java10;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
//文件复制
public class Test07 {
public static void main(String[] args) throws IOException {
copy("D:\\a", "D:\\b");
}
public static void copy(String src, String dest) throws IOException {
Path p1 = Paths.get(src);
Path p2 = Paths.get(dest);
//判断复制路径是否存在
if (!Files.exists(p1)) {
return;
} else {
//判断文件是否是目录
if (Files.isDirectory(p1)) {
Files.walkFileTree(p1, new FileVisitor<Path>() {
//创建文件的目录
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
//根据当前路径和给定的路径,构造两个路径之间的相对路径
Path rpath = p1.relativize(dir);
//两个路径相加
Path newPath = p2.resolve(rpath);
//判断新路径是否存在
if (!Files.exists(newPath)) {
Files.createDirectories(newPath);
}
return FileVisitResult.CONTINUE;
}
//创建对应的文件
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Path rpath = p1.relativize(file);
Path newPath = p2.resolve(rpath);
Files.copy(file, newPath);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
}
}
}
}
边栏推荐
- 初识网络层
- LVS 引入keepalived自动切换机制
- 4.1 脚本病毒编写实验
- 小米AX3600路由器扩容刷OpenWrt后,如何刷回官方系统和分区
- screen 乱码问题
- ARM Cortex-M debugging
- Sprinig Boot + Redis 实现接口幂等性,写得太好了
- 案例精述|Fortinet安全SD-WAN助力国际物流企业降本增效
- After the Xiaomi AX3600 router is expanded and flashed OpenWrt, how to flash back to the official system and partition
- shell脚本编写(4.函数调用)
猜你喜欢

4.5 远程入侵

看一眼就不会忘的 DNS解析服务

微信公众号开发流程指南

树莓派上Home Assistant 容器使用命令传感器获取并显示CPU温度、内存使用等信息

LVS虚拟服务器中负载均衡玩法

Explain in detail how to install Home Assistant Supervised on Raspberry Pi to make smart devices at home smarter

Serial USART and UART

Difference between ordinary factory class and abstract factory class

网安大事件丨Fortinet对Apache Log4j漏洞利用的全面复盘与防御

5.5 漏洞扫描:Web安全漏洞扫描及审计
随机推荐
3.2 定位shellcode
5.4 系统类型探测:主机系统识别
动态规划之数字n二进制位中1的个数
office运行时错误,部分系统文件可能丢失或已损坏(错误代码:0x80040154)
Explain in detail how to install Home Assistant Supervised on Raspberry Pi to make smart devices at home smarter
必看五大安全趋势 Fortinet全球威胁态势报告发布
教你怎样用三剑客玩转正则表达式
学好shell条件判断处事果断
3.3 关于代码植入和定位shellcode的思考
Docker 快速安装&搭建 Mysql 环境
Thread Basics
使用Curlftpfs挂载远程Ftp
一群菜鸟们的“员工信息管理系统”(C语言课程设计)
如何根据页面标签自动生成文章目录?分析+代码详解
CPU load average load average
KVM虚拟机管理(扩容,加CPU等)
The Home Assistant container on the Raspberry Pi uses the command sensor to obtain and display information such as CPU temperature, memory usage, etc.
mysql服务器参数设置总结
Fortinet :《2021 年OT与网络安全现状报告》之「OT安全洞察」
4.6 脚本及恶意网页攻击