First, the Java file Rename method is: Renameto ();
I put 180 pictures under d:\\backup and rename them with the following program:
Copy Code code as follows:
public void ReName () {
String dir = "d:\\backup\\";
File File = new file (dir);
String filename[] = File.list ();
int number = Filename.length;
File newfile[] = new File[number];
System.out.println ("number =" + number);
for (int i=0; i<number; i++) {
System.out.println ((i+1) + ":" + filename[i]);
Newfile[i] = new File (Dir+filename[i]);
}
System.out.println ("============ Split Line =============");
for (int i=0; i<number;i++) {
Boolean flag = Newfile[number-(i+1)].renameto (New File (dir + (i+1) + ". bmp"));
if (flag) {
System.out.println ("Rename success" + (i+1));
} else {
SYSTEM.OUT.PRINTLN ("failure");
}
}
}
The main reasons for Ps:java renaming failures are as follows:
1. The source file does not exist or is not writable.
2. The target file already exists.
3. The file format of different disks is the same when the source file and destination are not on the same disk.
The following is a file to be modified and modify the purpose of the file name in the array to rename the behavior, the comparison is not practical, should be the first parameter to the directory name is better, but the meaning of the line, ^-^
Copy Code code as follows:
/**
*
* @param from Save an array of file names that need to be modified
* @param to save the modified file name array
*/
public void Rename_1 (string from[], string to[]) {
int filelength = From.length;
Boolean flag = false;
Boolean isOk = false;
int count = 0;
if (filelength!= to.length) {
SYSTEM.OUT.PRINTLN ("Incoming array length inconsistent, so do not rename operation");
Return
}else{
for (int i=0;i<filelength;i++) {
for (int j=0;j<filelength;j++) {
if (From[i].equals (To[j])) {
Flag = false;
System.out.println ("error: The same file name exists, the system rejects the rename operation");
Return
}else{
Flag = true;
}
}
}
if (flag) {
System.out.println ("Start file Rename work");
for (int i=0;i<filelength;i++) {
isOk = new file (From[i]). Renameto (new file (To[i));
if (isOk) {
System.out.println ("Brother" + (i+1) + "File Rename successful");
count++;
}
}
if (count = = filelength) {
System.out.println ("All rename succeeded");
}else{
System.out.println ("There is a renamed unsuccessful file, please view the changes manually");
}
}
}
}