使用Path、Paths和DirectoryStream<T>查找出“指定格式”文件的方法分享
下文笔者讲述查找指定格式文件的方法分享,如下所示
查找出目录下指定的"java"文件
实现思路:
使用DirectoryStream即可查找出指定格式的文件
例:查找出目录下指定的"java"文件
package com.java265;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathDemo {
public static void main(String[] args) throws IOException {
PathDemo pathDemo = new PathDemo();
pathDemo.findFile("D:"+ File.separator+"test");
}
public void findFile(String dir) throws IOException {
Path path = Paths.get(dir);
DirectoryStream<Path> paths = Files.newDirectoryStream(path,"*.java");
for(Path p : paths){
System.out.println(p.getFileName());
}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


