Java implementation file rename

Source: Internet
Author: User

Recently, I am working on an android File Manager APK, which has the file name Rename and cut functions.

The general idea is as follows:

Rename: first create a new file, copy the original file, read and write the file, and finally Delete the original file.

Cut: first copy the original file and delete the original file

The above method has bugs. If the disk space is insufficient, an error may occur. At the same time, the file class in Java provides the duplicate name and cut function ., Why didn't I find it.

The following describes the methods in Java APIs.

Renameto
public boolean renameTo(File dest)
Rename the file represented by this abstract path name.

Many aspects of this method are related to the platform: the RENAME operation cannot move a file from one file system to another. This operation is not inseparable, if a file with the target abstract path name already exists, the operation may fail. Always check the return value to ensure that the RENAME operation is successful.

Parameters:
dest-Specify the new abstract path name of the file.
Return Value:
If the name is successfully renamed trueOtherwise false
Throw:
SecurityException-If a security manager exists
SecurityManager.checkWrite(java.lang.String)Method: deny write access to the original and new path names
NullPointerException-If the Parameter
destIs null

Note that the directory of the target file DEST must be stored.

However, there is another problem. Our company uses the MTK solution. Our mobile phone also has a default storage space and supports the expansion of sdcard. If file1 is cut to file2, in addition, if file1 and file2 are not in the same sdcard, the Operation will fail. I don't know why. If someone implements the same function, pay attention to it.

In this case, you can only use the above stupid method. However, before the operation, check the available space to prevent errors. This is what programmers do.

If you copy or cut a directory, that is, a folder, you need to traverse it cyclically. The loop folder is complicated ,.

Related Article

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.