Reputation: 7887
Apache Commons IO
has a method for moving files to another directory which doesn't allow me to specify a new name for it in the target directory:
public static void moveFileToDirectory(File srcFile, File destDir, boolean createDestDir)
throws IOException
I don't want to rename the file first, and then move the file to the other directory. Is there any way to do this via a single statement in any library method ? (I want a single method so as to avoid any concurrency issues, which will be automatically handled by the library)
Upvotes: 1
Views: 7329
Reputation: 7448
The Apache Commons IO FIleUtils class has the moveFile method. You simply need to specify the "new name" as part of the destFile
argument.
FileUtils.moveFile(
FileUtils.getFile("src/test/old-resources/oldname.txt"),
FileUtils.getFile("src/test/resources/renamed.txt"));
As Ian Roberts rightly says in his answer, you can just use the srcFile.renameTo(destFile)
method on the file if you know the destination exists, but the Commons IO class does the checking / folder creation for you. It uses FileUtils own copyFile method in the background if the renameTo fails (returns false) for any reason. If we look at the method comment, we clearly see "The directory holding the destination file is created if it does not exist".
This is the source of the FileUtils moveFile method, so that you can clearly see what it's providing for you:
2943 public static void moveFile(final File srcFile, final File destFile) throws IOException {
2944 if (srcFile == null) {
2945 throw new NullPointerException("Source must not be null");
2946 }
2947 if (destFile == null) {
2948 throw new NullPointerException("Destination must not be null");
2949 }
2950 if (!srcFile.exists()) {
2951 throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
2952 }
2953 if (srcFile.isDirectory()) {
2954 throw new IOException("Source '" + srcFile + "' is a directory");
2955 }
2956 if (destFile.exists()) {
2957 throw new FileExistsException("Destination '" + destFile + "' already exists");
2958 }
2959 if (destFile.isDirectory()) {
2960 throw new IOException("Destination '" + destFile + "' is a directory");
2961 }
2962 final boolean rename = srcFile.renameTo(destFile);
2963 if (!rename) {
2964 copyFile( srcFile, destFile );
2965 if (!srcFile.delete()) {
2966 FileUtils.deleteQuietly(destFile);
2967 throw new IOException("Failed to delete original file '" + srcFile +
2968 "' after copy to '" + destFile + "'");
2969 }
2970 }
2971 }
Upvotes: 5
Reputation: 122414
If you know the target directory exists already then you don't need anything external, just use srcFile.renameTo(destFile)
- the source and destination files don't have to be in the same directory.
Upvotes: 1
Reputation: 15708
Starting from Java7 you can use Files.move. E.g.
Path source = Paths.get(src);
Path target = Paths.get(dest);
Files.move(source, target, REPLACE_EXISTING, COPY_ATTRIBUTES);
Upvotes: 5
Reputation: 7391
Apache Commons has a lot of useful methods. For example you could use this one:
public void moveMyFile throws IOException {
FileUtils.moveFile(
FileUtils.getFile("path/to/source.txt"),
FileUtils.getFile("path/to/new destination.txt"));
}
Upvotes: 0