Go to GoReading for breaking news, videos, and the latest top stories in world news, business, politics, health and pop culture.

Deleting, Copying and Moving Files

106 4
An application may need to work with files in the filesystem. It might need to copy, delete, or move files and directories. By using a combination of the Path interface and the Filesclass file operations can be relatively simple to achieve. 

Delete Files and Directories


The Files class provides two methods for deleting a file or directory - delete and deleteIfExists.

The delete method will delete a file or directory if it can and throw and exception if it can't.

Likely exceptions that might be generated by trying to delete a file or directory are the NoSuchfileException, DirectoryNotEmptyException or a general IOException.

Always attempt to catch an exception with file operations:

Path p = Paths.get("/Users/writing/Desktop/Pics/Untitled56.txt");try {Files.delete(p);} catch (NoSuchFileException x) {//uh oh file does not exist} catch (DirectoryNotEmptyException x) {//hmm do we really want to delete a directory containing files?} catch (IOException x) {//catch all for other IO problems}
Note: The NoSuchFileException and DirectoryNotEmptyException are new exceptions introduced in Java 7. 

Or with the deleteIfExists method:

Path p = Paths.get("/Users/writing/Desktop/Pics/Untitled56.txt");try {Files.deleteIfExists(p);} catch (IOException x) {//catch all IO problems}
If the Path points to a symbolic link then the link itself is deleted not the file the symbolic link is pointing to.

Copying Files and Directories


The Files class provides a copy method for copying files or directories. You specify the original file and destination as a Path along with any copy options you want to enforce. There are three copy options available:
  • StandardCopyOption.REPLACE_EXISTING - normally if the target file already exists the copy operation will fail. If the REPLACE_EXISTING option is used then the existing file will be overwritten.
  • StandardCopyOption.COPY_ATTRIBUTES - copies the file attributes of the original file to the new copy.
  • LinkOption.NOFOLLOW_LINKS - if the Path being copied is a symbolic link this option will make the symbolic link be copied rather than the file it is pointing to.

For example,

Path original = Paths.get("/Users/writing/Desktop/Pics/Untitled56.txt"); //original filePath destination = Paths.get("/Users/writing/Desktop/Untitled56.txt"); //new filetry { Files.copy(original, destination, LinkOption.NOFOLLOW_LINKS);} catch (IOException x) { //catch all for IO problems}
Note: When a directory is being copied that is not empty only the directory itself is copied. The files are not. If you need the files to be copied to then do another copy operation for all the files you want to appear in the new directory.

Moving Files and Directories


Like the copy method the move method of the Files class has an original Path, a destination Path and move options that can be used. There are two move options available:
  • StandardCopyOption.REPLACE_EXISTING - use the REPLACE_EXISTING option if you want to move the file even if the target file already exists.
  • StandardCopyOption.ATOMIC_MOVE - uses an atomic file system operation.

For example,

Path original = Paths.get("/Users/writing/Desktop/Pics/Untitled56.txt");Path destination = Paths.get("/Users/writing/Desktop/Untitled56.txt");try { Files.move(original, destination, StandardCopyOption.REPLACE_EXISTING);} catch (IOException x) { //catch all for IO problems}
As you can see deleting, copying and moving files or directories is very simple using the Files class and Path interface.
Source...

Leave A Reply

Your email address will not be published.