使用 java 使用另一个列表的文件名重命名文件名列表

Rename filenames list with another list's filenames with java

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 名称存储在一个数组中,还有国家名称和正确的顺序。

我试过上面的这段代码,但它不起作用

我不会使用两个数组,而是使用一个 HashMap,其中键是国家 ISO 代码,值是相关的国家名称。喜欢:

String absolutePath = "/storage/emulated/0/Gadm/";
HashMap<String, String> countryCodes = new HashMap<>();
countryCodes.put("AFG","Afghanistan");
countryCodes.put("XAD","Akrotiri and Dhekelia");
countryCodes.put("ALA","Åland");
countryCodes.put("ZWE","Zimbabwe");

for(Map.Entry<String, String> entry : countryCodes.entrySet()) {
    File file = new File(absolutePath + entry.getKey());
    if (file.renameTo(new File(absolutePath + entry.getValue()))) {
        System.out.println("Successfully renamed " + entry.getKey() + " to " + entry.getValue());
    } else {
        System.out.println("Failed to rename " + entry.getKey() + " to " + entry.getValue() +
                ". Please make sure filepath exists: " + absolutePath + entry.getKey());
    }
}

作为替代方案,您可以使用 [=h11=] 而不是 File:

public static void rename(Path source) throws IOException {
    Map<String, String> countries = countries.get();

    Files.list(source)
         .filter(path -> Files.isRegularFile(path))
         .filter(path -> countries.containsKey(getFileName.apply(path)))
         .forEach(path -> {
             try {
                 Files.move(path, source.resolve(countries.get(getFileName.apply(path)) + getFileExt.apply(path)));
             } catch(IOException e) {
                 e.printStackTrace();
             }
         });
}

private static final Function<Path, String> getFileName = path -> {
    String fileName = path.getFileName().toString();
    return fileName.substring(0, fileName.lastIndexOf('.')).toUpperCase();
};

private static final Function<Path, String> getFileExt = path -> {
    String fileName = path.getFileName().toString();
    return fileName.substring(fileName.lastIndexOf('.'));
};

private static Supplier<Map<String, String>> countries = () -> {
    Map<String, String> map = new HashMap<>();
    map.put("AFG", "Afghanistan");
    map.put("XAD", "Akrotiri and Dhekelia");
    map.put("ALA", "Åland");
    map.put("ZWE", "Zimbabwe");
    return Collections.unmodifiableMap(map);
};

Client code is: rename(Paths.get("h:/gadm"))