1

我是一名尝试使用文件和目录的 Java 初学者。我想创建一个程序,我可以在其中自动更改文件名,同时在所有子目录中搜索无效的文件名。我实际上是在尝试将大量文件加载到服务器上,但服务器设置不允许包含特殊字符的文件名。首先,我能够编写代码,如果我将路径传递给目录,它会重命名该目录中所有名称无效的文件:

公共类重命名{

public static String baseLoc = "C:/Users/Developer/Desktop/.../Data Cleanup";

public static void main(String[] args) {

    //LinkedList<File> fileList = new LinkedList<File>();
    File obj = new File(baseLoc);
    int count = 0;

    for (File file: obj.listFiles())
    {

        String origName = file.getName();

        if (origName.contains("&")  || origName.contains("#") || origName.contains("@"))
        {
            System.out.println("Original name: "+origName);
            origName = origName.replaceAll("&", "_and_");
            origName = origName.replaceAll("@", "_at_");
            String newName = origName.replaceAll("#", "_");
            System.out.println("New Name: "+newName);
            String newLoc = baseLoc+"/"+newName;
            File newFile = new File(newLoc);
            System.out.println(file.renameTo(newFile));
            count++;
        }

    }
}

}

现在我想做同样的事情,但只是这次我希望所有文件都被重命名,即使在子目录中也是如此。有人可以指导我如何实现这一目标吗?

4

2 回答 2

1

递归是你的朋友

/**Removes 'invalid' characters (&,#,@) from pathnames in the given folder, and subfolders, and returns the number of files renamed*/
public int renameDirectory(File base){
    //LinkedList<File> fileList = new LinkedList<File>();

    int count=0;//count the renamed files in this directory + its sub. You wanted to do this?

    //Process each file in this folder.
    for (File file: base.listFiles()){

        String origName = file.getName();
        File resultFile=file;

        if (origName.contains("&")  || origName.contains("#") || origName.contains("@")){
            //I would replace the if statement with origName.matches(".*[&#@].*") or similar, shorter but more error prone.
            System.out.println("Original name: "+origName);
            origName = origName.replaceAll("&", "_and_");
            origName = origName.replaceAll("@", "_at_");
            String newName = origName.replaceAll("#", "_");
            System.out.println("New Name: "+newName);
            String newLoc = baseLoc+File.separator+newName;//having "/" hardcoded is not cross-platform.
            File newFile = new File(newLoc);
            System.out.println(file.renameTo(newFile));
            count++;
            resultFile=newFile;//not sure if you could do file=newFile, tired
        }

        //if this 'file' in the base folder is a directory, process the directory 
        if(resultFile.isDirectory()){//or similar function
            count+=renameDirectory(resultFile);
        }
    }
    return count; 
}
于 2013-10-30T15:23:44.750 回答
0

将您拥有的代码移至实用程序方法(例如public void renameAll(File f){})。有一个条件来检查文件是否是一个目录并用它的内容递归地调用你的方法。之后做你目前正在做的事情。

public void renameAll(File[] files){

    for(File f: files){
        if(f.isDirectory){
           renameAll(f.listFiles());
        }
        rename(f);

    }

}

public void rename(File f){ }
于 2013-10-30T15:17:59.877 回答