Java - 计算和重命名文件名

时间:2014-12-03 16:34:24

标签: java

我必须读取文件夹中的文件并重命名它们。在这种情况下,名称中有3个带有parrot的文件,4个带有joker,4个带有嗡嗡声。

我可以使用子字符串获取嗡嗡声,鹦鹉和嗡嗡声。但我必须经历它们并计算每次发生的次数,然后将它们命名为01,02,03,......一旦它被分组。我很难接受它。任何建议都有帮助。

原始文件名

apple_orange_buzz_banana01.txt
apple_orange_buzz_banana02.txt
apple_orange_buzz_banana05.txt
apple_orange_buzz_banana06.txt
apple_orange_joker_banana03.txt
apple_orange_joker_banana04.txt
apple_orange_joker_banana07.txt
apple_orange_joker_banana10.txt
apple_orange_parrot_banana08.txt
apple_orange_parrot_banana09.txt
apple_orange_parrot_banana11.txt

重命名文件名

buzz01.txt
buzz02.txt
buzz03.txt
buzz04.txt

joker01.txt
joker02.txt
joker03.txt
joker04.txt

parrot01.txt
parrot02.txt
parrot03.txt

谢谢

3 个答案:

答案 0 :(得分:1)

您可以使用Map存储每种文件(buzz,joker,...)和正则表达式的计数器,以查找您感兴趣的名称部分。

所以你的代码看起来像

Pattern p = Pattern.compile("apple_orange_(\\w+)_banana\\d+[.]txt");
Map<String, Integer> map = new HashMap<>();

File dir = new File("d:/zzz");//path to directory containing your files
File[] files = dir.listFiles();//you can provide file filter here
Arrays.sort(files);//lets make sure that files are sort alphabetically

for (File f : files) {
    Matcher m = p.matcher(f.getName());
    if (m.find()) {
        String name = m.group(1);
        int counter = map.getOrDefault(name, 0) + 1;
        map.put(name, counter);
        f.renameTo(new File(dir, String.format("%s%02d.txt", name, counter)));
    }
}

答案 1 :(得分:0)

只需按字母顺序对文件进行排序,然后将文件名拆分为数组并重命名:

//get list of files
String path = "C:/Users/your/folder/";
File folder = new File(path);
File[] listOfFiles = folder.listFiles();

//this will sort alphabetically
java.util.Collections.sort(listOfFiles);

String mainPart = "";
int counter = 1;

for (File file : listOfFiles) {
    if (file.isFile()) {
        //get parts
        String[] parts = file.getName()).split("_");

        //note: make sure file name has consistent formatting
        if(!mainPart.equals(parts[2])){
            mainPart = parts[2];
            counter = 1;
        }

        //keep track if the number needs a leading zero.
        String zero = ""
        if(counter < 10)
            zero = "0";

        //note: check to see if renameTo was successful
        file.renameTo(new File(path + mainPart + zero + counter + ".txt"));
        counter++;      
    }
}

我还没有测试过这段代码,因此可能会出现轻微的语法错误。但是你明白了。

答案 2 :(得分:0)

我将创建一个Map,并使用新文件前缀作为键以及要重命名为值的文件列表。然后循环它们重命名它们。

不确定文件列表的位置,但您可以从中获得要点:

List<File> files = new ArrayList<File>();

files.add(new File("apple_orange_buzz_banana01.txt"));
files.add(new File("apple_orange_buzz_banana02.txt"));
files.add(new File("apple_orange_buzz_banana05.txt"));
files.add(new File("apple_orange_buzz_banana06.txt"));


files.add(new File("apple_orange_joker_banana03.txt"));
files.add(new File("apple_orange_joker_banana04.txt"));
files.add(new File("apple_orange_joker_banana07.txt"));
files.add(new File("apple_orange_joker_banana10.txt"));

files.add(new File("apple_orange_parrot_banana08.txt"));
files.add(new File("apple_orange_parrot_banana09.txt"));
files.add(new File("apple_orange_parrot_banana11.txt"));


Map<String,List<File>> prefixMap = new HashMap<String,List<File>>();

for (File f : files) {
    String[] splits = f.getName().split("_");

    if (splits.length == 4) {
        String prefix = splits[2];
        if (!prefixMap.containsKey(prefix)) {
            prefixMap.put(prefix, new ArrayList<File>());
        }
        prefixMap.get(prefix).add(f);
    }
}

// Now rename them
for (String p : prefixMap.keySet()) {
    int counter = 1;

    for (File f : prefixMap.get(p)) {
        f.renameTo(new File(p+String.format("%02d", counter)+".txt"));
        counter++;
    }
}