Java File renaming (file batch renaming) instance program code sharing _java

Source: Internet
Author: User
Tags array length

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");
}
}
}
}

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.