当前位置:网站首页>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();
}
}
});
}
边栏推荐
- [Shanda conference] peer connection based on webrtc
- SAP 脚本教程:SE71、SE78、SCC1、VF03、SO10-013
- 【LeetCode】9、回文数
- 静态断言 static_assert
- Dear students, don't read the textbooks any more. Just read this one for the complexity of time
- 买网红雪糕的我,成了大冤种
- 【山大会议】使用TypeScript为项目进行重构
- [译文] 弥合开源数据库和数据库业务之间的鸿沟
- Swift -- save print log to sandbox
- SAP ABAP 内部表:创建、读取、填充、复制和删除-06
猜你喜欢

【小程序项目开发-- 京东商城】uni-app开发之配置tabBar & 窗口样式

Static assertion static_ assert

实现一个Container全局组件步骤(给还不会使用组件的新手一个思路,大佬绕道)

谷歌浏览器的小细节

什么是 SAP ABAP? 类型、ABAP 完整形式和含义

数睿数据受邀参与南通企业数字化转型研讨会

84. (cesium chapter) movement of cesium model on terrain

SAP ABAP 中的 Smart Forms-014
![[译文] 弥合开源数据库和数据库业务之间的鸿沟](/img/e5/f89a8f3e2e9034f557ea3e76f37f81.jpg)
[译文] 弥合开源数据库和数据库业务之间的鸿沟

学习量子纠缠的可解释表示,该深度生成模型可直接应用于其他物理系统
随机推荐
3. abstract class (shape)
北京恢复堂食半月记:如何重燃门店经营烟火气
SAP价值流程&帮助请求流程-011
Research on ICT: domestic databases focus on the ICT market, and Huawei Gauss is expected to become the strongest
【华为云至简致远】征文获奖名单出炉!
Cmake tutorial series-00-introduction
The difference between nvarchar and varchar
[Shanda conference] acquisition of user media based on webrtc
New design of databend SQL planner
【山大会议】多人视频通话 WebRTC 工具类搭建
phantomJs使用总结
SAP ABAP 中 OpenSQL和Native SQL-05 本教程的目的不是教您 SQL 或数据库概念
2.接口(计算器)
安全信得过!天翼云数据安全管理平台通过评测
Alibaba cloud middleware's open source past
3.抽象類(shape)
Odoo local document function development record
Google Chrome small details
Gbase "library" special training of innovation and application Committee of Beijing fintech Industry Alliance
天翼云乘风新基建,构建数字化转型“4+2”能力体系