1

I'm trying to write a function that takes a keyword and searches through a list of files then prints out any of the files that contain the key word.

So far all I have is a list of files and the keyword.

File[] files = new File("<directory>").listFiles();
Scanner keyword = new Scanner("hello");

I think now I need to construct some form of loop that goes through the files looking for the keyword. Any help of even an easy to follow tutorial is appreciated.

edit:

The files are text files that only consist of one line

user2148423
  • 51
  • 1
  • 5
  • Here is a concise example of reading a file: http://stackoverflow.com/a/3806154/16959 – Jason Sperske Mar 20 '13 at 18:19
  • Of course if you want to read the text content of a file, take a look at http://tika.apache.org/, it is a library that can extract text from Word Docs, Web pages (avoiding HTML tags), PDFs, etc – Jason Sperske Mar 20 '13 at 18:21
  • 1
    Although it is quite possible to do this in Java, have you considered (depending on your requirements) using one of the standard command line tools to do this? The UNIX grep utility does exactly what you need for example. – codebox Mar 20 '13 at 18:21
  • I know about grep but I need it to be in Java – user2148423 Mar 20 '13 at 18:22
  • Why do you need to do in Java - is this homework? – Amir Afghani Mar 20 '13 at 18:44
  • No its not homework. I've written pretty much the same in Python but want to see whats more efficient, python or java – user2148423 Mar 20 '13 at 18:51

2 Answers2

3
File dir = new File("directory"); // directory = target directory.
if(dir.exists()) // Directory exists then proceed.
{ 
  Pattern p = Pattern.compile("keyword"); // keyword = keyword to search in files.
  ArrayList<String> list = new ArrayList<String>(); // list of files.

  for(File f : dir.listFiles())
  {
    if(!f.isFile()) continue;
    try
    {
      FileInputStream fis = new FileInputStream(f);
      byte[] data = new byte[fis.available()];
      fis.read(data);
      String text = new String(data);
      Matcher m = p.matcher(text);
      if(m.find())
      {
        list.add(f.getName()); // add file to found-keyword list.
      }
      fis.close();
    } 
    catch(Exception e)
    {
      System.out.print("\n\t Error processing file : "+f.getName());
    }

  }
  System.out.print("\n\t List : "+list); // list of files containing keyword.
} // IF directory exists then only process.
else
{
  System.out.print("\n Directory doesn't exist.");
}
VishalDevgire
  • 4,232
  • 10
  • 33
  • 59
0

if you want to use scanner class, here is how you can scan a file for a specific keyword: Scanner is nothing but an iterator which scans through the input provided to it.

Scanner s = new Scanner(new File("abc.txt"));
while(s.hasNextLine()){
    //read the file line by line
String nextLine = s.nextLine();
            //check if the next line contains the key word
    if(nextLine.contains("keyword"))
    {
              //whatever you want to do when the keyword is found in the file
               and break after the first occurance is found
             break;
    }
}
CoderP
  • 1,361
  • 1
  • 13
  • 19