Reputation: 407
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.
Upvotes: 0
Views: 719
Reputation: 26
You could read the files and use .contains("specific word") (a string method that will return true if the string contains that specific word).
Upvotes: 1