How to rename file in Java [ Ubuntu platform ]?

时间秒杀一切 提交于 2020-06-29 11:38:35

问题


I want to rename a file in Java. My working platform is Ubuntu. The renameFile() didn't work for me. Is there any other ways to rename a file ?

ADDED :

File file = new File( "/home/test/filename.txt" );

if ( file.renameTo( new File( "/home/test/modified.txt" ) ) ) {
    System.out.println( "Rename succesful" );
} else {
    System.out.println( "Rename failed" );
}

回答1:


You can use File.renameTo(newFileName)

If this doesn't work, you need to work out why as this method isn't very helpful. e.g. you can't rename a files which isn't there or you don't have permissions to the directories.



来源:https://stackoverflow.com/questions/18721750/how-to-rename-file-in-java-ubuntu-platform

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