Wednesday, August 18, 2010

Recursively listing all the files in directory java

/**
* Get all the files in a directory. This recursively gets the files from
* sub directories as well
*
* @param parentDir
* @return
* @throws FileNotFoundException
*/
private static List getFiles(File parentDir)
throws FileNotFoundException {
List result = new ArrayList();
File[] filesAndDirs = parentDir.listFiles();
List filesDirs = Arrays.asList(filesAndDirs);
for (File file : filesDirs) {
result.add(file);
if (file.isFile() == false) {
List deeperList = getFiles(file);
result.addAll(deeperList);
}
}
return result;
}

Using Apache Lucene Example.

Recently I downloaded apache lucene project and struggled to run their examples. Probably this blog my help you to save 3hr of effort.

Aim: if you want to search a word inside the files. Please follow below steps

1. Index the directory first
2. Create a query file
3. Search the directory

• Indexing the dir ‘workspace’
o java -cp lucene-core-3.0.2.jar;lucene-demos-3.0.2.jar org.apache.lucene.demo.IndexFiles C:\workspace
• Create a query file “queries.txt” and save all the keys you want to search. For Example ‘hello’
o Search for ‘hello’ word in all the files under dir ‘workspace’
• Search using lucene
o java -cp lucene-core-3.0.2.jar;lucene-demos-3.0.2.jar org.apache.lucene.demo.SearchFiles -index C:\index C:\ queries.txt

queries.txt file should have string ‘hello’. If you want to search multiple words have multiple words in the file separated by \n

Followers