在文件夹中查找与模式匹配的最新文件
Find latest file that matches pattern in folder
我正在编写一个需要 2 个输入的方法:
String name
String path
然后输出最新的以name(是一个变量)开头且在路径中的pdf(以pdf为扩展名)文件名。
我正在使用:
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 方法,因为它是一个变量并且每次都会更改。
将名称更改为名为 name
的变量之一。在您的方法中使用 final
标记 String name
参数(或它将具有的任何名称)以便在匿名 class 中使用并直接使用它。
代码应如下所示:
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 ...
}
我正在编写一个需要 2 个输入的方法:
String name
String path
然后输出最新的以name(是一个变量)开头且在路径中的pdf(以pdf为扩展名)文件名。
我正在使用:
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 方法,因为它是一个变量并且每次都会更改。
将名称更改为名为 name
的变量之一。在您的方法中使用 final
标记 String name
参数(或它将具有的任何名称)以便在匿名 class 中使用并直接使用它。
代码应如下所示:
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 ...
}