0
我正在嘗試編寫一個代碼,用於搜索所選文件夾中所有包含文件名中包含特定單詞的文檔/圖形擴展名的文件。我找不到任何similiar例子,我試過這個1是最接近的幾種不同的方法:在java中使用擴展名和通配符搜索進行遞歸文件搜索
private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
File startingPath = dirChooser.getCurrentDirectory();
String[] extensions = new String[] {
"txt", "doc", "docx", "jpg", "jpeg", "pdf", "odt", "png", "bmp"};
// there are about 20 different patterns like the ones below
Pattern p = Pattern.compile(".*password.*|.*user.*|.*profile.*");
List<File> files = (List<File>) FileUtils.listFiles(
startingPath, extensions, true);
files.forEach((file) -> {
if (file.isFile()){
Matcher m = p.matcher(file.toString().toLowerCase());
if (m.matches()) {
filesArea.append(file.toString() + "\n");
noOfFiles++;
}
}
});
}
的擴展搜索的工作完美,但出於某種原因,模式的搜索只適用於某些文件夾,而不是別人。我相信有更好的方法,但這似乎是最簡單和(幾乎)工作。任何想法如何讓它充分發揮作用?
對不起,延遲迴復,它的工作,謝謝! – momp