集册 Java实例教程 使用FileVisitor复制文件

使用FileVisitor复制文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

605
使用FileVisitor复制文件

import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;


import java.io.IOException;/* from 时   代     Java  公  众  号 - nowjava.com*/

import java.nio.file.FileSystemLoopException;

import java.nio.file.FileVisitOption;

import java.nio.file.FileVisitResult;

import java.nio.file.FileVisitor;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.attribute.BasicFileAttributes;

import java.nio.file.attribute.FileTime;

import java.util.EnumSet;


class CopyTree implements FileVisitor<Object> {/* 来自 nowjava*/

  Path copyFrom;

  Path copyTo;

  public CopyTree(Path copyFrom, Path copyTo) {

    this.copyFrom = copyFrom;

    this.copyTo = copyTo;

  }


  static void copySubTree(Path copyFrom, Path copyTo) throws IOException {

    try {

      Files.copy(copyFrom, copyTo, REPLACE_EXISTING, COPY_ATTRIBUTES);

    } catch (IOException e) {

      System.err.println("Unable to copy " + copyFrom + " [" + e + "]");

    }

  }

  @Override

  public FileVisitResult postVisitDirectory(Object dir, IOException exc)

      throws IOException {

    if (exc == null) {

      Path newdir = copyTo.resolve(copyFrom.relativize((Path) dir));

      try {

        FileTime time = Files.getLastModifiedTime((Path) dir);

        Files.setLastModifiedTime(newdir, time);

      } catch (IOException e) {

        System.err.println("Unable to copy all attributes to: " + newdir + " ["

            + e + "]");

      }

    } else {

      throw exc;

    }


    return FileVisitResult.CONTINUE;

  }


  @Override

  public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs)

      throws IOException {

    System.out.println("Copy directory: " + (Path) dir);

    Path newdir = copyTo.resolve(copyFrom.relativize((Path) dir));

    try {

      Files.copy((Path) dir, newdir, REPLACE_EXISTING, COPY_ATTRIBUTES);

    } catch (IOException e) {

      System.err.println("Unable to create " + newdir + " [" + e + "]");

      return FileVisitResult.SKIP_SUBTREE;

    }


    return FileVisitResult.CONTINUE;

  }


  @Override

  public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)

      throws IOException {

    System.out.println("Copy file: " + (Path) file);

    copySubTree((Path) file, copyTo.resolve(copyFrom.relativize((Path) file)));

    return FileVisitResult.CONTINUE;

  }


  @Override

  public FileVisitResult visitFileFailed(Object file, IOException exc)

      throws IOException {

    if (exc instanceof FileSystemLoopException) {

      System.err.println("Cycle was detected: " + (Path) file);

    } else {

      System.err.println(
展开阅读全文