//实现功能:把文件夹1里的文件按照文件夹2里的名字一一对应命名,并移动保存到文件夹3中
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import javax.net.ssl.SSLException;
public class GetFileName
{
static String A4 = "fakeA4";
static String moveEye = "fakemove";
static String copper = "fakecropper";
public static String [] getFileName(String path)
{
File file = new File(path);
String [] fileName = file.list();
return fileName;
}
/**
*
* @param oldname 保存需要修改的文件名数组
* @param newname 保存修改后的文件名数组
*/
public static void reName(String oldname[], String newname[]){
int fileLength = oldname.length;
boolean flag = false;
boolean isOk = false;
int count = 0;
if(fileLength != newname.length){
System.out.println("传入数组长度不一致,故不进行重命名操作");
return;
}else{
for(int i=0;i<fileLength;i++){
for(int j=0;j<fileLength;j++){
if(oldname[i].equals(newname[j]) ){
flag = false;
System.out.println("error:存在相同文件名,系统拒绝重命名操作");
return;
}else{
flag = true;
}
}
}
if(flag){
System.out.println("开始进行文件重命名工作》》》");
for(int i=0;i<fileLength;i++){
File f1 = new File("E:\\data\\ipad\\"+oldname[i]);
File f2 = new File("E:\\data\\ipad+\\"+newname[i]+".mov");
isOk = f1.renameTo(f2);
// System.out.println("source file is exist? " + f1.exists()
// + ", source file => " + f1);
// System.out.println(f2 + " is exist? " +f2.exists());
if(isOk){
System.out.println("第" + (i+1) +"个文件重命名成功");
count++;
}
}
if(count == fileLength){
System.out.println("全部重命名成功");
}else{
System.out.println("存在重命名未成功的文件,请人工查看更改");
}
}
}
}
public static void main(String[] args)
{
int n = 9;
int m = 4;
String [] finalName = null;
String [] fileName = getFileName("E:\\data\\data");
String [] oldfileName = getFileName("E:\\data\\ipad");
ArrayList arrayList = new ArrayList();
for(String name:fileName)
{
String str_id=name.substring(name.length()-n,name.length()-m);//第一个参数是开始截取的位置,第二个是结束位置。
int id = Integer.valueOf(str_id).intValue();
Allname alln = new Allname(name, id);
arrayList.add(alln);
}
Object[] names =arrayList.toArray();
//把排序规则交给sort方法。该方法就会按照自定义的规则进行排序
java.util.Arrays.sort(names,new MyComparator());
finalName = new String [names.length];
for (int i = 0; i<names.length;i = i+3){
String name = names[i].toString();//将Object类型转化为String类型
String str_id=name.substring(name.length()-n,name.length()-m);
int id = Integer.valueOf(str_id).intValue();
String name1 = A4+id;
String name2 = moveEye+id;
String name3 = copper+id;
finalName[i] = name1;
finalName[i+1] = name2;
finalName[i+2] = name3;
}
reName(oldfileName,finalName);
}
}
class Allname{
String name;
int id ;
public Allname(String name,int id){
this.name = name;
this.id = id;
}
public String toString(){
return name;
}
}
class MyComparator implements Comparator{
public int compare(Object obj1, Object obj2 ){
Allname u1 = (Allname)obj1;
Allname u2 = (Allname)obj2;
if(u1.id>u2.id){
return 1;
}else if(u1.id<u2.id){
return -1;
}else{
//利用String自身的排序方法。
//如果年龄相同就按名字进行排序
return u1.name.compareTo(u2.name);
}
}
}