Renaming a file without using renameTo() - Java

懵懂的女人 提交于 2019-11-29 10:14:13

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.

big lep

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:

I think you are confusing things. A java.util.File doesn't represent a file on some filesystem. It represents a path to a file.

The problem is not that a symlink is involved; the problem is that you can't atomically rename across filesystems. The meta-problem is that the Java File operations are badly designed, and don't throw proper exceptions, and provide no error codes when something fails!

How about:

a.renameTo(new File("/your/path/here/");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!