String filePattern = "/data/logs/.+\\.log";
File f = new File(filePattern);
File parentDir = f.getParentFile();
String regex = f.getName();
FileSystem FS = FileSystems.getDefault();
final PathMatcher matcher = FS.getPathMatcher("regex:" + regex);
DirectoryStream.Filter<Path> fileFilter = new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
return matcher.matches(entry.getFileName()) && !Files.isDirectory(entry);
}
};
List<File> result = Lists.newArrayList();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(parentDir.toPath(), fileFilter)) {
for (Path entry : stream) {
result.add(entry.toFile());
}
} catch (IOException e) {
e.printStackTrace();
}
for(File file : result) {
System.out.println(file.getParent() + "/" + file.getName());
}
Path path = Paths.get("/data/logs");
Pattern pattern = Pattern.compile("^.+\\.log");
List<Path> paths = Files.walk(path).filter(p -> {
//如果不是普通的文件,则过滤掉
if(!Files.isRegularFile(p)) {
return false;
}
File file = p.toFile();
Matcher matcher = pattern.matcher(file.getName());
return matcher.matches();
}).collect(Collectors.toList());
for(Path item : paths) {
System.out.println(item.toFile().getPath());
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有