Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java file.renameTo() does rename file but returns false. Why?

Tags:

java

file-io

The problem is that I need the file to move before the rest of my logic will work so when the method returns false I stop execution.

However, when I check on the file in windows explorer it has a new name and it moved.

Just curious why this is happening.

here is some sample code I just tried to recreate the issue. It's pretty much the same thing and it's working fine.

File testfile = new File("TestFile");

    if(!testfile.exists()){

        testfile.mkdirs();

    }

    File sample = new File("sample.txt");

    if(sample.exists()){

        boolean success = sample.renameTo(new File(testfile.getPath() + "\\" + sample.getName()));

        if(success){

            System.out.println("Moved");

        }
        else{

            System.out.println("Failed");

        }

    }

Edit: Solved it. I'm sorry for wasting everyone's time with something so silly. However, I really dont think I would have tracked this down if not for making this post.

The solution was that I was actually looping through several files to move. When the output said it failed then the program stopped and when I looked in explorer only the first of the files was actually moved so I assumed it was moving and then returning false. However, the issue was that I was using the wrong variable as an index and so what was happeneing was that it did successfully move the file in index 0 and then when the loop repeated the index didnt increment so it tried to move index 0 again and therefore failed.

Like I said, very stupid but thanks for bearing with me.

like image 693
Mason Avatar asked Feb 01 '13 17:02

Mason


People also ask

Why does renameTo return false?

As you can see in this example, the renameTo() method returns a boolean value indicating the renaming succeeded (true) or failed (false) - so you should always check its return value. If the destination file exists, the method returns false. If the destination directory exists, the method return false.

How does renameTo work in Java?

The renameTo() method is used to rename the abstract pathname of a file to a given pathname. The method returns a boolean value i.e. returns true if the file is renamed else returns false. Create an object of the File class and replace the file path with the path of the directory.

How does file renameTo work?

File renameTo() method in Java with examples Parameters: The function requires File object destination as parameter, the new abstract path name of the present file. Exception: This method throws following exceptions: Security Exception if the method does not allow write operation of the abstract pathnames.

Which method is used to rename the file in Java?

In Java we can rename a file using renameTo(newName) method that belongs to the File class. Parameters: dest – The new abstract pathname for the existing abstract pathname.


2 Answers

Java's File.renameTo() is problematic, especially on Windows, it seems. As the API documentation says:

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

You can use apache.commons.io library, which includes FileUtils.moveFile() or also the Files.move() method in JDK 7.

like image 189
Gabriele Mariotti Avatar answered Oct 25 '22 17:10

Gabriele Mariotti


Isn't it possible that you file has a Inputstream open somewhere but has not been closed and so the rename is not working. Try closing all open streams relevant to the file object before closing.

like image 24
Anugoonj Avatar answered Oct 25 '22 17:10

Anugoonj