简体   繁体   English

Java:重命名文件-无法正确重命名文件

[英]Java: Rename Files - Not renaming files correctly

The following Java code renames files but it does not rename the way I wanted it to. 以下Java代码重命名文件,但未重命名我想要的文件。 I want the rename files to start from beginning in the directory to the end per the “part” numbers (not the time it was added in inside a particular directory), but it does not do so. 我希望重命名文件按照“部分”的编号从目录的开头开始到结尾(而不是它在特定目录中添加的时间)开始,但是不这样做。 Is there an “if” check I can put, like: if filename.contains(j) then process this? 我是否可以放入“ if”支票,例如: if filename.contains(j)然后进行处理?

import java.io.File;

public class RenameFile2 {

    public static void main(String[] args) {

        String absolutePath1 = "1 to 4";
        String absolutePath2 = "6 to 9";
        String absolutePath3 = "10 to 99";
        String absolutePath4 = "100 to 224";

        File dir1 = new File(absolutePath1);
        File dir2 = new File(absolutePath2);
        File dir3 = new File(absolutePath3);
        File dir4 = new File(absolutePath4);

        File[] filesInDir1 = dir1.listFiles();
        File[] filesInDir2 = dir2.listFiles();
        File[] filesInDir3 = dir3.listFiles();
        File[] filesInDir4 = dir4.listFiles();

        int i1 = 1;

        for(File file:filesInDir1) {
            String name = file.getName();
            String newName = "550 00510 00" + i1 + ".pdf";
            String newPath = absolutePath1 + "\\" + newName;
            file.renameTo(new File(newPath));
            System.out.println(name + " changed to " + newName);
            i1++;
        }

        int i2 = 6;
        for(File file:filesInDir2) {
            String name = file.getName();
            String newName = "550 00510 00" + i2 + ".pdf";
            String newPath = absolutePath2 + "\\" + newName;
            file.renameTo(new File(newPath));
            System.out.println(name + " changed to " + newName);
            i2++;
        }

        int i3 = 10;
        for(File file:filesInDir3) {
            String name = file.getName();
            String newName = "550 00510 0" + i3 + ".pdf";
            String newPath = absolutePath3 + "\\" + newName;
            file.renameTo(new File(newPath));
            System.out.println(name + " changed to " + newName);
            i3++;
        }

        int i4 = 100;
        //int j = 99;
        for(File file:filesInDir4) {
            String name = file.getName();
/*            if(name.contains(j))
                {
                }
*/
            String newName = "550 00510 " + i4 + ".pdf";
            String newPath = absolutePath4 + "\\" + newName;
            file.renameTo(new File(newPath));
            System.out.println(name + " changed to " + newName);
            i4++;
            }
        }
    }

You need to sort the files once you list them, the listFiles() does not guarentee any order. 列出文件后,您需要对文件进行排序, listFiles()不能保证任何顺序。

File[] filesInDir1 = dir1.listFiles();
Arrays.sort(filesInDir1);

You may need to implement a Comparator and call Arrays.sort(filesList, comparator) in order to have them custom sorted. 您可能需要实现一个Comparator并调用Arrays.sort(filesList, comparator)才能对它们进行自定义排序。

Once you have the files in sorted order, you can apply the rename logic as you wanted. 一旦文件具有排序顺序,就可以根据需要应用重命名逻辑。

Another improvement would be, if you want to list only files with a pattern, you can filter while listing. 另一个改进是,如果您只想列出带有模式的文件,则可以在列出时进行过滤。

final String pattern = "regexpattern";
 dir1.listFiles(new FilenameFilter(){
                           public boolean accept( File dir, String name ) {
                               return name.matches( pattern );
                           }
                        });

In the documentation for .listFiles() , it says: .listFiles()文档中 ,它说:

There is no guarantee that the name strings in the resulting array will appear in any specific order; 不能保证结果数组中的名称字符串会以任何特定顺序出现; they are not, in particular, guaranteed to appear in alphabetical order. 尤其不能保证它们按字母顺序出现。

You need to sort the array in the order you want to process them in a particular order. 您需要按照要按特定顺序处理数组的顺序对数组进行排序。


Additional notes and suggestions: 其他注意事项和建议:

  • String.contains() takes a String as an argument, not an int . String.contains()String用作参数,而不是int You have this code commented out in your question. 您已在问题中注释了此代码。
  • Use the constructor for File to produce the new path instead of string concatenation: File newPath = new File(dir1, newName); 使用File的构造函数来生成新路径,而不是字符串连接: File newPath = new File(dir1, newName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM