gpt4 book ai didi

java - 在Java中用不同的名称重命名多个文件夹

转载 作者:太空宇宙 更新时间:2023-11-04 09:54:44 29 4
gpt4 key购买 nike

使用此代码,我可以重命名两个文件夹:

   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...

最佳答案

将所有文件夹名称放入一个数组中,并循环遍历该数组以重命名所有文件夹。

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

}

}

关于java - 在Java中用不同的名称重命名多个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54305777/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com