
In this post you can find some code examples written in the java language to perform common file operations.
Renaming a file
The base class java.io.File implements a renameTo method that can be used to rename a file.
For instance, renaming a file “data.txt” to “data.csv” can be done with the following sentences:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.io.File; public class rename_file { public static void main(String args[]) throws java.io.IOException { File file1 = new File("data.txt"); File file2 = new File("data.csv"); boolean success = file1.renameTo(file2); if (!success) { System.out.println("Error trying to rename file"); } } } |
As we can see, the renameTo() method just returns true if the operation has been successfully carried out, and false otherwise.
But there are several reasons that could cause a failure in the renaming operation: The source file does not exist, the user has not enough privileges to rename the file, there exists a file with the same destination name, etc…
To determine the cause of the error, some sentences can be added before the call to renameTo. For instance, to check that the source file exists and the destination file does not exist:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
File file1 = new File("data.txt"); if (!file1.exists()) { System.out.println("Error. source file \"data.txt\" not found"); return; } File file2 = new File("data.csv"); if(file2.exists()) { System.out.println("Error. Destination file \"data.csv\" already exists"); return; } |
Moving a file to a different directory
The renameTo() method in class java.io.File can also be used to move a file to another directory, if the filename of the destination file includes a path (absolute or relative) to the new directory.
But renameTo() fails is the destination directory is in a different filesystem or disk than the source file.
In Java 6, this issue can be solved by making firs a copy of the file to the destination directory (as explained below), and then removing the original with a call to the delete() method in java.io.File.
Copy a file in Java 6
As already mentioned, there is no “copy()” method in the base libraries of Java 6.
But a function to perform the copy can be easily written using class java.nio.channels.FileChannel:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.io.IOException; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; public static void copyFile(File sourceFile, File destFile) throws IOException { if(!destFile.exists()) { destFile.createNewFile(); } FileChannel origin = null; FileChannel destinatiion = null; try { origin = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); long count = 0; long size = origin.size(); while((count += destination.transferFrom(origin, count, size-count))<size); } finally { if(origin != null) { origin.close(); } if(destination != null) { destination.close(); } } } |
Copy a file in Java 7
In Java 7, there is a class java.nio.file.Files that implements a set of static methods to perform most common file handling operations. Among them there is a copy() method that can be used to copy a file, even if the destination directory is in a different filesystem.
The copy() method accepts an argument of class CopyOptions, that can be used to specify:
If the destination file is to be overwritten in case it exists.
If attributes (including permissions) or the source file are to be replicated in the destination file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.io.IOException; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; import java.nio.file.CopyOption; import java.nio.file.StandardCopyOption; import java.nio.file.Paths; import java.nio.file.Path; import java.nio.file.Files; public static void copyFile_Java7(String origin, String destination) throws IOException { Path FROM = Paths.get(origin); Path TO = Paths.get(destination); //overwrite the destination file if it exists, and copy // the file attributes, including the rwx permissions CopyOption[] options = new CopyOption[]{ StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES }; Files.copy(FROM, TO, options); } |
References:
—
Index of posts related to Java programming
—