I have a task to search all .txt files for a specific word and then print all files containing that word. I have this code which finds all .txt files, and I just need to add check if that word is in, but with using BufferedReader import. I'm new in Java and I'd be happy if you help me.
package thenewboston.tutorials;
import java.io.File;
import java.io.FilenameFilter;
import java.io.BufferedReader;
public class apples30 {
public static void main(String[] args) {
File folder = new File("D:\\test");
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if(name.lastIndexOf('.')>0) {
int lastIndex = name.lastIndexOf('.');
String str = name.substring(lastIndex);
if(str.equals(".txt")) {
return true;
}
}
return false;
}
};
File[] listOfFiles = folder.listFiles(filter);
for(File x: listOfFiles) {
System.out.println(x.getName());
}
}
}
This is how my code looks now.
您可以阅读文件并使用.contains(“ specific word”)(一个字符串方法,如果该字符串包含该特定单词,则将返回true)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.