Disregarding my last post, I've found the source of the problem. I'm using
a.renameTo(b)
when b doesn't exist. The reason it doesn't exist is because there is a symbolic link so if b is /usr/name/folder/file
, then b really is /mnt/MountTest
because the symlink is to that directory.
So the question is, is there an alternative way to rename a file in Java using a string value? If not, how can this rename procedure be done differently?
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.
To rename or move a file/directory in Java, you can use either the renameTo() method of a File object in the old File I/O API, or the Files. move() method in the new Java NIO API.
A rename would rename it... if it were on the same filesystem.
If a renameTo() fails, you'll need to copy it to the new location, then delete the original.
Renaming files is also highly problematic accross file systems. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4073756. Commenters of the bug report posted some sample code and also pointed out that you can use Process.exec. Both Apache Commons IO and and Google Guava have utilities for safely moving files as well:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With