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.
Renametopublic 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
true Otherwise
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
dest Is
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 ,.