Java Reference
In-Depth Information
Apart from the REPLACE_EXISTING CopyOption , you can use the ATOMIC_MOVE as another CopyOption .
If the ATOMIC_MOVE option is used, it throws an AtomicMoveNotSupportedException if the file could not be moved
atomically. If ATOMIC_MOVE option is specified, all other options are ignored. The following snippet of code shows how
to move a file by handling possible exceptions:
import java.io.IOException;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardCopyOption.ATOMIC_MOVE;
...
// Create source and target paths using the syntax supoprted by your platform
Path source = Paths.get("C:\\poems\\luci1.txt");
Path target = Paths.get("C:\\poems\\dir2\\luci1.txt");
try {
// Try moving the source to target atomically
Path p = Files.move(source, target, ATOMIC_MOVE);
System.out.println(source + " has been moved to " + p);
}
catch (NoSuchFileException e) {
System.out.println("Source/target does not exist.");
}
catch (FileAlreadyExistsException e) {
System.out.println(target + " already exists. Move failed.");
}
catch (DirectoryNotEmptyException e) {
System.out.println(target + " is not empty. Move failed.");
}
catch (AtomicMoveNotSupportedException e){
System.out.println("Atomic move is not supported. MOve failed.");
}
catch (IOException e) {
e.printStackTrace();
}
Commonly Used File Attributes
The Files class has many methods that let you access the commonly used attributes of a file. For example, you can
use the Files.isHidden(Path p) method to test if a file is hidden. The following methods in the Files class let you
access various types of commonly used attributes of a file. Please refer to the “Managing File Attributes” section for
managing advanced file attributes.
 
Search WWH ::




Custom Search