使用此代码,我可以重命名两个文件夹:
public static void main(String[] args)
{
RenameFolder f = new RenameFolder();
f.RenameFolder();
}
private void RenameFolder()
{
File f1= new File("C:\\Users\\Nm\\Desktop\\Lauer");
File f2= new File("C:\\Users\\Nm\\Desktop\\Axeler");
try {
if(f1.exists()) {
f1.renameTo(f2);
System.out.println("Folder " +f1.getName()+
" was changed into " +f2.getName() +"..." );
} else {
f1.mkdir();
System.out.println("Folder " +f1.getName()+ " was created..." );
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
当我必须重命名数千个具有名称的文件夹时,该怎么办?例如:
raro -> noto
mano -> kaoto
Daum -> Loeme
Gato -> Rate
Ta+To -> Mo~no
etc...
答案 0 :(得分:0)
将所有文件夹名称放入数组中,并在数组中循环以重命名所有文件夹。
private void RenameFolder()
{
ArrayList CurrentName=new ArrayList();
ArrayList NewName=new ArrayList();
CurrentName.add(path);
NewName.add(path);
//repeat about two lines for as many folders you want.
for(int i=0;i<CurrentName.size()-1;i++){
File f1= new File(CurrentName.get(i));
File f2= new File(NewName.get(i));
try {
if(f1.exists()) {
f1.renameTo(f2);
System.out.println("Folder " +f1.getName()+
" was changed into " +f2.getName() +"..." );
} else {
f1.mkdir();
System.out.println("Folder " +f1.getName()+ " was created..." );
}
} catch(Exception e) {
e.printStackTrace();
}
}
}