Atomically move and rename an instance of Path

Given the instance Path path, I have the following questions:

  • How to rename a file in which points path, without resorting to the old API File, if possible, I still could not find it?

  • Is it possible to physically move a physical file to a new directory and rename it at the same time?

I am using Java 8, but new things for the class pathhave been added for sure, but are not sure if any help can answer this question.

+4
source share
2 answers

Regarding your first question, since Java 7 you can use Files#move:

Files.move(path, targetPath);

, , ATOMIC_MOVE:

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

Files.move(path, targetPath, ATOMIC_MOVE);

, :

  • AtomicMoveNotSupportedException, (, ).
  • REPLACE_EXISTING, , , , , , IOException.
+5

- , , , Files.move(Path source, Path target, CopyOption... options):

.

StandardCopyOption.ATOMIC_MOVE :

, . , , , IOException. , AtomicMoveNotSupportedException. , , FileStore .

+1

Source: https://habr.com/ru/post/1532632/


All Articles