我有一個java類和 我想讀取名稱以「home-search-log.log」開頭的文件。所以我寫了這個代碼。正則表達式來匹配文件名
File dir = new File("D:\\");
File[] foundFiles = dir.listFiles(new FileFilter() {
public boolean accept(File file) {
return Pattern.compile("^(home-search-log.log)").matcher(file.getName()).matches();
}
});
for (File file : foundFiles) {
System.out.println(file.getName());
}
但它只返回一個具有給定名稱的文件。我的其他文件的名稱是「home-search-log.log.2013-04-12」,但它不會被此模式返回。我錯在哪裏?
您是否搜索過SO?有很多以前的答案 –