使用Files.walkFileTree遍历目录文件
java.NIO.file.Files.walkFileTree是jdk7新增的静态工具方法。
1.Files.walkFileTree的原理介绍
static Path walkFileTree(Path start, Set<FileVisitOption> options, int maxDepth, FileVisitor<? super Path> visitor) throws IOException;
static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException;
参数列表:
java.nio.file.Path start
遍历的起始路径Set<java.nio.file.FileVisitOption> options
遍历选项int maxDepth
遍历深度java.nio.file.FileVisitor<? super Path> visitor
遍历过程中的行为控制器
2.遍历行为控制器FileVisitor
接口java.nio.file.FileVisitor包含四个方法,涉及到遍历过程中的几个重要的步骤节点。
一般实际中使用SimpleFileVisitor简化操作。
public interface FileVisitor<T> {
FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs)
throws IOException;
FileVisitResult visitFile(T file, BasicFileAttributes attrs)
throws IOException;
FileVisitResult visitFileFailed(T file, IOException exc)
throws IOException;
FileVisitResult postVisitDirectory(T dir, IOException exc)
throws IOException;
}
preVisitDirectory
访问一个目录,在进入之前调用。postVisitDirectory
一个目录的所有节点都被访问后调用。遍历时跳过同级目录或有错误发生,Exception会传递给这个方法visitFile
文件被访问时被调用。该文件的文件属性被传递给这个方法visitFileFailed
当文件不能被访问时,此方法被调用。Exception被传递给这个方法。
3.遍历行为结果 FileVisitResult
public enum FileVisitResult {
CONTINUE,
TERMINATE,
SKIP_SUBTREE,
SKIP_SIBLINGS;
}
CONTINUE
继续遍历SKIP_SIBLINGS
继续遍历,但忽略当前节点的所有兄弟节点直接返回上一层继续遍历SKIP_SUBTREE
继续遍历,但是忽略子目录,但是子文件还是会访问TERMINATE
终止遍历
4.查找指定文件
使用java.nio.file.Path提供的startsWith、endsWith等方法,需要特别注意的是:匹配的是路径节点的完整内容,而不是字符串。
例如: /usr/WEB/bbf.jar
Path path = Paths.get("/usr/web/bbf.jar");
path.endsWith("bbf.jar"); // true
path.endsWith(".jar"); // false
5.使用PathMatcher
@Test
public void visitFile2() throws IOException {
// 查找java和txt文件
String glob = "glob:**
@Test
public void visitFile1() throws IOException {
String path = "D:\\work_java\\hty\\HTY_CORE";
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
String pathStr = file.toString();
if (pathStr.endsWith("properties") || pathStr.endsWith("html")) {
System.out.println(file);
}
return FileVisitResult.CONTINUE;
}
});
}
@Test
public void visitFile2() throws IOException {
String glob = "glob:**
@Test
public void visitFile3() throws IOException {
// (?i)忽略大小写,(?:)标记该匹配组不应被捕获
String reg = "regex:.*\\.(?i)(?:properties|html)";
String path = "D:\\work_java\\hty\\HTY_CORE";
final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(reg);
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if (pathMatcher.matches(file)) {
System.out.println(file);
}
return FileVisitResult.CONTINUE;
}
});
}
6.查找指定文件
@Test
public void visitFile() throws IOException {
String path = "D:\\work_java\\hty\\HTY_CORE\\src";
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
// 使用endsWith,必须是路径中的一段,而不是几个字符
if (file.endsWith("log.java")) {
System.out.println(file);
// 找到文件,终止操作
return FileVisitResult.TERMINATE;
}
return FileVisitResult.CONTINUE;
}
});
}
7.遍历单层目录
使用DirectoryStream会获取指定目录下的目录和文件。可以使用newDirectoryStream的第二个参数进行筛选,glob语法。
@Test
public void dir() throws IOException {
Path source = Paths.get("D:\\work_java\\hty\\HTY_CORE\\src\\main\\resources");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(source, "*.xml")) {
Iterator<Path> ite = stream.iterator();
while (ite.hasNext()) {
Path pp = ite.next();
System.out.println(pp.getFileName());
}
}
}
8.复制文件到新目录
@Test
public void copyAll() throws IOException {
Path source = Paths.get("D:\\work_java\\hty\\HTY_CORE\\src");
Path target = Paths.get("D:\\temp\\core");
// 源文件夹非目录
if (!Files.isDirectory(source)) {
throw new IllegalArgumentException("源文件夹错误");
}
// 源路径的层级数
int sourcePart = source.getNameCount();
Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
// 在目标文件夹中创建dir对应的子文件夹
Path subDir;
if (dir.compareTo(source) == 0) {
subDir = target;
} else {
// 获取相对原路径的路径名,然后组合到target上
subDir = target.resolve(dir.subpath(sourcePart, dir.getNameCount()));
}
Files.createDirectories(subDir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, target.resolve(file.subpath(sourcePart, file.getNameCount())),
StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
System.out.println("复制完毕");
}
9.文件和流的复制
@Test
public void copy1() throws IOException {
Path source = Paths.get("D:\\work_java\\hty\\HTY_CORE\\src\\main\\resources\\ehcache.xml");
Path target = Paths.get("D:\\temp\\");
if (!Files.exists(target)) {
Files.createDirectories(target);
}
Path targetFile = target.resolve(source.getFileName());
try (InputStream fs = FileUtils.openInputStream(source.toFile())) {
Files.copy(fs, targetFile, StandardCopyOption.REPLACE_EXISTING);
}
}
@Test
public void copy2() throws IOException {
Path source = Paths.get("D:\\work_java\\hty\\HTY_CORE\\src\\main\\resources\\ehcache.xml");
Path target = Paths.get("D:\\temp\\core");
Path targetFile = target.resolve(source.getFileName());
if (!Files.exists(target)) {
Files.createDirectories(target);
}
try (OutputStream fs = FileUtils.openOutputStream(targetFile.toFile());
OutputStream out = new BufferedOutputStream(fs)) {
Files.copy(source, out);
}
}
10.Path与File的转换
@Test
public void testPath() {
File file = new File("D:\\work_java\\hty\\HTY_CORE");
System.out.println(file.toURI());//file:/D:/work_java/hty/HTY_CORE
System.out.println(file.getAbsolutePath());//D:\work_java\hty\HTY_CORE
System.out.println(file.getName());//HTY_CORE
System.out.println("-------");
//File转换为Path
Path path = Paths.get(file.toURI());
System.out.println(path.toUri());//file:///D:/work_java/hty/HTY_CORE
System.out.println(path.toAbsolutePath());//D:\work_java\hty\HTY_CORE
System.out.println(path.getFileName());//HTY_CORE
System.out.println("-------");
//Path转换为File
File f = path.toFile();
System.out.println(f.getAbsolutePath());//D:\work_java\hty\HTY_CORE
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
相关文章