Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a file to another file system?

Tags:

java

file

rename

I got a strange problem on using renameTo(). I don't know why I can't rename to /mnt/desttest but it is ok to rename to /home/kit.ho/desttest. However, I already give every write permission to /mnt/. The return value is false while no specific reason. Who knows the reason?

import java.io.File;
public class renameFile {
    public static void main(String[] args) {
        File sourceFile = new File("/home/kit.ho/test");  
        File targetFile1 = new File("/mnt/desttest");  
        System.out.println("source file is exist? " + sourceFile.exists() + ", source file => " + sourceFile);  
        System.out.println(targetFile1 + " is exist? " + targetFile1.exists());  
        System.out.println("rename to " + targetFile1 + " => " + sourceFile.renameTo(targetFile1));  
        System.out.println("source file is exist? " + sourceFile.exists() + ", source file => " + sourceFile);   
    }
}

Edit: Finally, based on some answers, Rename function does not work on across file system, is there any workaround on this issue by not calling external command like "mv"?

like image 501
TheOneTeam Avatar asked Dec 07 '22 19:12

TheOneTeam


1 Answers

You can't do a rename across file systems (partitions).

like image 90
jdigital Avatar answered Dec 10 '22 11:12

jdigital