亲宝软件园·资讯

展开

使用Files.walkFileTree遍历目录文件

阿拉的梦想 人气:0

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;

参数列表:

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;
}

3.遍历行为结果 FileVisitResult

public enum FileVisitResult {
    CONTINUE,
    TERMINATE,
    SKIP_SUBTREE,
    SKIP_SIBLINGS;
}

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:**/*.{java,txt}";
  String path = "D:\\work_java\\bbf\\CORE";

  final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob);

  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;
    }
  });
}

getPathMatcher方法的参数语法:规则:模式,其中规则支持两种模式glob和regex。

5.1全局规则glob

使用类似于正则表达式但语法更简单的模式,匹配路径的字符串。

5.1.1规则说明

5.2正则规则regex

使用java.util.regex.Pattern支持的正则表达式。

5.2.1示例

获取指定扩展名的文件

以下测试用例,目的都是获取指定目录下的.properties和.html文件。

/**
 * 递归遍历,字符串判断
 *
 * @throws IOException IO异常
 */
@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;
    }
  });
}

/**
 * 递归遍历,glob模式
 *
 * @throws IOException IO异常
 */
@Test
public void visitFile2() throws IOException {
  String glob = "glob:**/*.{properties,html}";
  String path = "D:\\work_java\\hty\\HTY_CORE";

  final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob);

  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;
    }
  });
}

/**
 * 递归遍历,正则模式
 *
 * @throws IOException IO异常
 */
@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.查找指定文件

/**
 * 查找指定文件
 *
 * @throws IOException IO异常
 */
@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语法。

/**
 * 遍历单层目录
 *
 * @throws IOException IO异常
 */
@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.复制文件到新目录

/**
 * 递归复制
 *
 * @throws IOException IO异常
 */
@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.文件和流的复制

/**
 * 流复制到文件
 *
 * @throws IOException IO异常
 */
@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);
  }
}

/**
 * 文件复制到流
 *
 * @throws IOException IO异常
 */
@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的转换

/**
 * 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
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论