Renaming files in Java

Source: Internet
Author: User

1.File oldf = new File (oldfilename);
File NEWF =null;

if (oldf!= null) {
NEWF = new File (newfilename);
Oldf.renameto (NEWF);
}

2. I used to think that the File#renameto (file) method is the same as the MOVE/MV command below the OS, so that you can achieve the purpose of renaming and moving files. However, often found that the problem: the File#renameto (file) method will return failure (false), the file did not move, and found no reason, and then simply discard the method, their own implementation of a copy method, the problem has never appeared.

Yesterday the boss classmate again encountered this problem, File#renameto (File) method works under Windows Good, under Linux occasionally again failed. I went home, I swept the source code of the File#renameto (File) method in the JDK, and found that it was calling a local method (native methods) and could not be traced. Some people on the internet say that the method is normal under Windows and is not normal under Linux. This is hard to say, Sun could not have made such a platform inconsistent code out AH.

Later on in Sun's official forum, I saw someone mention the issue "works on Windows, don ' t work on Linux," and someone later replied that "file systems" is different. How could it not be the same?

In the back of a forum to find someone on the issue of the elaboration:

In the Unix ' esque o/s ' 's You cannot renameto () across file systems. This behavior is different than the Unix "MV" command. When crossing file systems MV does a copy and delete which are what you'll have to do if it's the case.

The same thing would happen on Windows if you tried to renameto a different drive, i.e. C:-> D:

Finally understand.

Do an experiment:

  

The following is a code fragment:
1 File sourcefile = new file ("C:/test.txt");
2 File TargetFile1 = new file ("E:/test.txt");
3 File TargetFile2 = new file ("D:/test.txt");
4 System.out.println ("Source file is exist?" + sourcefile.exists () + ", source file =>" + sourcefile);
5 System.out.println (TargetFile1 + "is exist?" + targetfile1.exists ());
6 System.out.println ("Rename to" + TargetFile1 + "=>" + Sourcefile.renameto (targetFile1));
7 System.out.println ("Source file is exist?" + sourcefile.exists () + ", source file =>" + sourcefile);
8 System.out.println (TargetFile2 + "is exist?" + targetfile2.exists ());
9 System.out.println ("Rename to" + TargetFile2 + "=>" + Sourcefile.renameto (targetFile2));
Results:

  

This is a code fragment:
1 source file is exist? True, source file => c:\test.txt
2 e:\test . txt is exist?   False
3 Rename to E:\test.txt => false
4 source file is exist? True, source file => c:\test.txt
5 d:\test.txt is exist? False
6 Rename to D:\test.txt => true

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.