To Search a word in in list of files available in Folder, you need to find the list of files first and then scan each and every for required word. Below is the sample program to find the a given word Java in D:\\test folder of files.
package in.javatutorials;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.MatchResult;/**
* @author malliktalksjava
*
* Search for the files in a folder and prints all file details.
*
*/
public class WordCrawlerInFolder {private static String directoryPath = “D://test”;
private static String searchWord = “Java”;
/**
* Creating constructor
*/
public WordCrawlerInFolder() {
super();
}/**
* main method
*
* @param ags
*/
public static void main(String[] args) {
WordCrawlerInFolder crawler = new WordCrawlerInFolder();
File directory = new File(directoryPath);if (directory == null || !directory.exists()) {
System.out.println(“Directory doesn’t exists!!!”);
return;
}crawler.directoryCrawler(directory, searchWord);
}/**
* Gets all the file and directories and prints accordingly
*
* @param directory
* Directory path where it should search
*/
public void directoryCrawler(File directory, String searchWord) {// Get List of files in folder and print
File[] filesAndDirs = directory.listFiles();// Print the root directory name
//System.out.println(“-” + directory.getName());// Iterate the list of files, if it is identified as not a file call
// directoryCrawler method to list all the files in that directory.
for (File file : filesAndDirs) {if (file.isFile()) {
searchWord(file, searchWord);
//System.out.println(” |-” + file.getName());
} else {
directoryCrawler(file, searchWord);
}
}
}/**
* Search for word in a given file.
* @param file
* @param searchWord
*/
private void searchWord(File file, String searchWord){
Scanner scanFile;
try {
scanFile = new Scanner(file);
while (null != scanFile.findWithinHorizon(“(?i)\\b”+searchWord+”\\b”, 0)) {
MatchResult mr = scanFile.match();
System.out.printf(“Word found : %s at index %d to %d.%n”, mr.group(),
mr.start(), mr.end());
}
scanFile.close();
} catch (FileNotFoundException e) {
System.err.println(“Search File Not Found !!!!! “);
e.printStackTrace();
}
}
}
We have used some escape characters in above class searchWord() method, below is the notation for the same.
- (?i) turn on the case-insensitive switch
- \b means a word boundary
- java is the string searched for
- \b a word boundary again.
If search term contain special characters, it would be suggested to use \Q and \E around the string, as it quotes all characters in between. Make sure the input doesn’t contain \E itself.
Other Useful Links:
Javac/Java searching algorithm for other classes
Example program to reverse a Number in Java
How to find count of duplicates in a List
Threads Interview questions in Java
Filed under: JAVA Tagged: Example program to search a word in JAva, File directory, How to search a word in file using java, How to write a search functionality in java, how to write a searching logic in java, Java, Java Example Programns, Java examples, Java sample Programs, Java Sample Search Programs, Java Tutorials, Search word in file sample program in java, searchWord
