当前位置:网站首页>nio文件和文件夹操作例子
nio文件和文件夹操作例子
2022-06-22 15:10:00 【原力与你同在】
nio文件和文件夹操作例子
统计一个文件夹下特定文件的数目
public static void main(String[] args) throws IOException {
// 统计文件夹出现的数目
AtomicInteger dirCount = new AtomicInteger();
// 统计文件出现的数目
AtomicInteger fileCount = new AtomicInteger();
Files.walkFileTree(Paths.get("D:\\soft\\java\\jdk"),new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
dirCount.getAndIncrement();
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String string = file.toString();
if(string.endsWith(".jar")){
fileCount.getAndIncrement();
}
return super.visitFile(file, attrs);
}
});
System.out.println("dir count: "+dirCount);
System.out.println("file count: "+fileCount);
}
输出:
dir count: 136
file count: 724
删除文件和文件夹
public static void main(String[] args) throws IOException {
Files.walkFileTree(Paths.get("D:\\a"),new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return super.visitFile(file, attrs);
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return super.postVisitDirectory(dir, exc);
}
});
}
多级别文件复制
public static void main(String[] args) throws IOException {
String source = "D:\\a";
String target = "D:\\b";
Files.walk(Paths.get(source)).forEach(path->{
String targetName = path.toString().replace(source,target);
if(Files.isDirectory(path)){
try {
Files.createDirectory(Paths.get(targetName));
} catch (IOException e) {
e.printStackTrace();
}
}
else if(Files.isRegularFile(path)){
try {
Files.copy(path,Paths.get(targetName));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
边栏推荐
- Program substitution function
- 畅享高性能计算!天翼云HPC解决方案来了
- [Shanda conference] definitions of some basic tools
- Turn to: jackwelch: strategy is to think less and be quick to act
- [Shanda conference] software performance optimization and bug repair
- 84.(cesium篇)cesium模型在地形上运动
- [Shangshui Shuo series] day three - VIDEO
- 使用枚举实现工厂模式
- SAP 中的 ABAP 查询教程:SQ01、SQ02、SQ03-017
- Simulation Implementation of string
猜你喜欢
随机推荐
买网红雪糕的我,成了大冤种
【山大会议】私人聊天频道 WebRTC 工具类
SAP ABAP 报告编程-08
84. (cesium chapter) movement of cesium model on terrain
Unity游戏优化(第2版)学习记录8
Wechat applet avatar pendant production
Pymssql Module User Guide
Runtime -- explore the nature of classes, objects, and classifications
[Shanda conference] application setting module
Differences between Oracle client and server
首个赛博格人陨落背后:科技与渐冻症的极限赛跑
Odoo local document function development record
The difference between nvarchar and varchar
Oracle客户端和服务端的区别
jmeter关联登录302类型的接口
Mr. Du built a domestic non garlic Statistics Platform
Navicat premium connecting to Oracle database (Graphic tutorial)
[Shanda conference] definitions of some basic tools
nio编程service
知识管理在业务中的价值如何体现









