1

我正在编写一个需要 2 个输入的方法:

  1. String name

  2. String path

然后输出最新的 pdf(以 pdf 作为扩展名)文件名,该文件名以 name(这是一个变量)开头并且在路径中。

我在用:

public String getLatestMatchedFilename(String path, String name){
    File dir=new File(path);    
    File[] files = dir.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.startsWith();
        }
    });
}

但是,我不知道如何将 name 中的值传递给 accept 方法,因为它是一个变量并且每次都会更改。

4

1 回答 1

0

Change name to one of the variables called name. Mark the String name parameter (or whatever name it will have) in your method with final in order to be used inside an anonymous class and use it directly.

Here is how the code should look:

public String getLatestMatchedFilename(String path, final String name) {
    File dir = new File(path);    
    File[] files = dir.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String nameFilter) {
            return nameFilter.startsWith(name);
        }
    });
    // rest of your code ...
}
于 2015-02-11T20:40:28.100 回答