1、Path

jdk7 引入了 Path 和 Paths 类

  • Path 用来表示文件路径
  • Paths 是工具类,用来获取 Path 实例
    ```java Path source = Paths.get(“1.txt”); // 相对路径 使用 user.dir 环境变量来定位 1.txt

Path source = Paths.get(“d:\1.txt”); // 绝对路径 代表了 d:\1.txt

Path source = Paths.get(“d:/1.txt”); // 绝对路径 同样代表了 d:\1.txt

Path projects = Paths.get(“d:\data”, “projects”); // 代表了 d:\data\projects

  1. <a name="c91V3"></a>
  2. # 2、Files
  3. **检查文件是否存在**
  4. ```java
  5. Path path = Paths.get("helloword/data.txt");
  6. System.out.println(Files.exists(path));

创建一级目录

  1. Path path = Paths.get("helloword/d1");
  2. Files.createDirectory(path);
  • 如果目录已存在,会抛异常 FileAlreadyExistsException
  • 不能一次创建多级目录,否则会抛异常 NoSuchFileException

创建多级目录

  1. Path path = Paths.get("helloword/d1/d2");
  2. Files.createDirectories(path);

拷贝文件

  1. Path source = Paths.get("helloword/data.txt");
  2. Path target = Paths.get("helloword/target.txt");
  3. Files.copy(source, target);
  • 如果文件已存在,会抛异常 FileAlreadyExistsException

如果希望用 source 覆盖掉 target,需要用 StandardCopyOption 来控制

  1. Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);

移动文件

  1. Path source = Paths.get("helloword/data.txt");
  2. Path target = Paths.get("helloword/data.txt");
  3. Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
  • StandardCopyOption.ATOMIC_MOVE 保证文件移动的原子性

删除文件

  1. Path target = Paths.get("helloword/target.txt");
  2. Files.delete(target);
  • 如果文件不存在,会抛异常 NoSuchFileException


删除目录**

  1. Path target = Paths.get("helloword/d1");
  2. Files.delete(target);
  • 如果目录还有内容,会抛异常 DirectoryNotEmptyException

遍历目录

  1. public static void visitDir(String targetDir) throws IOException {
  2. AtomicInteger dirCount = new AtomicInteger(0);
  3. AtomicInteger fileCount = new AtomicInteger(0);
  4. Files.walkFileTree(Paths.get(targetDir),new SimpleFileVisitor<>(){
  5. @Override
  6. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  7. System.out.println("======>" + dir);
  8. dirCount.addAndGet(1);
  9. return super.preVisitDirectory(dir, attrs);
  10. }
  11. @Override
  12. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  13. System.out.println(file);
  14. fileCount.addAndGet(1);
  15. return super.visitFile(file, attrs);
  16. }
  17. });
  18. System.out.println("目录数量:" + dirCount);
  19. System.out.println("文件数量:" + fileCount);
  20. }

删除多级目录

  1. public static void delDir(String targetDir) throws IOException {
  2. Files.walkFileTree(Paths.get(targetDir),new SimpleFileVisitor<>(){
  3. @Override
  4. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  5. System.out.println("======>" + dir);
  6. return super.preVisitDirectory(dir, attrs);
  7. }
  8. @Override
  9. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  10. System.out.println(file);
  11. Files.delete(file);
  12. return super.visitFile(file, attrs);
  13. }
  14. @Override
  15. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  16. System.out.println("<======" + dir);
  17. Files.delete(dir);
  18. return super.postVisitDirectory(dir, exc);
  19. }
  20. });
  21. }

拷贝目录

  1. public static void copyDir(String sourceDir,String targetDir) throws IOException {
  2. Files.walk(Paths.get(sourceDir)).forEach(path -> {
  3. try {
  4. String newPath = path.toString().replace(sourceDir, targetDir);
  5. if(Files.isDirectory(path)){
  6. Files.createDirectory(Paths.get(newPath));
  7. }else if(Files.isRegularFile(path)){
  8. Files.copy(path,Paths.get(newPath));
  9. }
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }
  13. });
  14. }