[NIO.2] 第三十七篇 编写一个文件移动应用
程序员文章站
2022-04-25 10:01:19
...
移动文件分为两个步骤,先拷贝文件,再删除源文件。
下面的代码将会把 C:\rafaelnadal 目录中的内容移动到 C:\ATP\players\rafaelnafal 目录中。在移动前,要确保 C:\ATP\players\rafaelnafal 目录已经存在。在这个例子中,目录的移动使用了 Files.copy() 和 Files.delete() 方法,文件的移动使用了 Files.move() 方法。
文章来源:http://www.aptusource.org/2014/04/nio-2-writing-a-move-files-application/
下面的代码将会把 C:\rafaelnadal 目录中的内容移动到 C:\ATP\players\rafaelnafal 目录中。在移动前,要确保 C:\ATP\players\rafaelnafal 目录已经存在。在这个例子中,目录的移动使用了 Files.copy() 和 Files.delete() 方法,文件的移动使用了 Files.move() 方法。
import java.io.IOException; 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; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES; import static java.nio.file.StandardCopyOption.ATOMIC_MOVE; class MoveTree implements FileVisitor { private final Path moveFrom; private final Path moveTo; static FileTime time = null; public MoveTree(Path moveFrom, Path moveTo) { this.moveFrom = moveFrom; this.moveTo = moveTo; } static void moveSubTree(Path moveFrom, Path moveTo) throws IOException { try { Files.move(moveFrom, moveTo, REPLACE_EXISTING, ATOMIC_MOVE); } catch (IOException e) { System.err.println("Unable to move " + moveFrom + " [" + e + "]"); } } @Override public FileVisitResult postVisitDirectory(Object dir, IOException exc) throws IOException { Path newdir = moveTo.resolve(moveFrom.relativize((Path) dir)); try { Files.setLastModifiedTime(newdir, time); Files.delete((Path) dir); } catch (IOException e) { System.err.println("Unable to copy all attributes to: " + newdir+" [" + e + "]"); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException { System.out.println("Move directory: " + (Path) dir); Path newdir = moveTo.resolve(moveFrom.relativize((Path) dir)); try { Files.copy((Path) dir, newdir, REPLACE_EXISTING, COPY_ATTRIBUTES); time = Files.getLastModifiedTime((Path) dir); } catch (IOException e) { System.err.println("Unable to move " + newdir + " [" + e + "]"); return FileVisitResult.SKIP_SUBTREE; } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException { System.out.println("Move file: " + (Path) file); moveSubTree((Path) file, moveTo.resolve(moveFrom.relativize((Path) file))); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Object file, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } } class Main { public static void main(String[] args) throws IOException { Path moveFrom = Paths.get("C:/rafaelnadal"); Path moveTo = Paths.get("C:/ATP/players/rafaelnadal"); MoveTree walk = new MoveTree(moveFrom, moveTo); EnumSet opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS); Files.walkFileTree(moveFrom, opts, Integer.MAX_VALUE, walk); } } 当然,也可以不使用 Files.move() 方法,而使用 Files.copy() 和 Files.delete() 方法来移动文件: static void moveSubTree(Path moveFrom, Path moveTo) throws IOException { try { Files.copy(moveFrom, moveTo, REPLACE_EXISTING, COPY_ATTRIBUTES); Files.delete(moveFrom); } catch (IOException e) { System.err.println("Unable to move " + moveFrom + " [" + e + "]"); } }
文章来源:http://www.aptusource.org/2014/04/nio-2-writing-a-move-files-application/
上一篇: 华硕ZenPad系列平板配置曝光