简体   繁体   中英

renaming file while renaming directory

Trying to rename both directory name and file name.

try
    {
        File dir = new File("DIR");
        dir.mkdir();
        File file1 = new File(dir,"myfile1.txt");
        file1.createNewFile();
        File file2 = new File(dir,"myfile2.txt");
        file2.createNewFile();

        dir.renameTo(new File("myDIR"));            
        System.out.print(file1.renameTo(new File(dir,"myf1.txt")));
    }
    catch(IOException ie)
    {

    }

However,only directory is being successfully renamed and not the filename.
can these operations not be done simultaneously?

This is because your dir , file1 and file2 are pointing to the old path.

After these lines are executed,

File dir = new File("DIR");
dir.mkdir();
File file1 = new File(dir,"myfile1.txt");
file1.createNewFile();
File file2 = new File(dir,"myfile2.txt");
file2.createNewFile();

these will be the paths referenced by the variables,

dir = "DIR" // Exists
file1 = "DIR\myfile1.txt" //Exists
file2 = "DIR\myfile2.txt" //Exists

After you execute,

    dir.renameTo(new File("myDIR"));            

the paths referenced by the variables are still the same,

dir = "DIR" // Doesn't exist anymore because it's moved.
file1 = "DIR\myfile1.txt" // Doesn't exist anymore because it's moved along with dir.
file2 = "DIR\myfile2.txt" // Doesn't exist anymore because it's moved along with dir.

So, when you call,

    System.out.print(file1.renameTo(new File(dir,"myf1.txt")));

You are calling renameTo() on a file which doesn't exist and to a directory which doesn't exist either. So it's bound to fail.

Even if you call .exists() method on any of dir , file1 or file2 , it would return false only.

I found this question very interesting! The sequence of the program always matters.

try
{
    File dir = new File("DIR");
    dir.mkdir();

Here dir points to a location in file system.

    File file1 = new File(dir,"myfile1.txt");
    file1.createNewFile();
    File file2 = new File(dir,"myfile2.txt");
    file2.createNewFile();

When you rename it, it means that dir will point to a different location.

    dir.renameTo(new File("myDIR"));  

You are trying to rename a file which points to a location that has gone obsolete.

    System.out.print(file1.renameTo(new File(dir,"myf1.txt")));
}
catch(IOException ie)
{
    System.out.println(ie);
}

Try the below code, I have moved the code to rename the folder after file rename.

try
{
    File dir = new File("DIR");
    dir.mkdir();

    File file1 = new File(dir,"myfile1.txt");
    file1.createNewFile();
    File file2 = new File(dir,"myfile2.txt");
    file2.createNewFile();   
    System.out.print(file1.renameTo(new File(dir,"myf1.txt")));         

    dir.renameTo(new File("myDIR"));
}
catch(IOException ie)
{
    System.out.println(ie);
}

I tested the code!

Not like this. After you have renamed the dir, file1 and file2 objects still points to the old file path before the remaining. You need to set them to the "new" files in the renamed dir.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM