【发布时间】:2018-11-30 19:38:10
【问题描述】:
import java.io.*;
public class Main {
public static void main(String[] args) {
// change file names in 'Directory':
String absolutePath = "/storage/emulated/0/Gadm";
File dir = new File(absolutePath);
File[] filesInDir = dir.listFiles();
int i = 0;
for(File file:filesInDir) {
i++;
String[] iso = {
"AFG",
"XAD",
"ALA",
"ZWE"};
String[] country = {
"Afghanistan",
"Akrotiri and Dhekelia",
"Åland",
"Zimbabwe"};
String name = file.getName();
String newName = name.replace(iso[i],country[i]);
String newPath = absolutePath + "/" + newName;
file.renameTo(new File(newPath));
System.out.println(name + " has been changed to " + newName);
}
}
}
我有一个名为 Gadm 的目录,它包含一个文件列表,名称如下,后跟国家/地区的 iso 代码,例如 iso.kmz 我将使用其对应的国家/地区名称重命名所有文件名,以成为 country.kmz
iso 名称以正确的顺序存储在数组中,还有国家名称。
我尝试了上面的代码,但它不起作用
【问题讨论】: