Solved

how find specific words in a file

Posted on 2008-06-12
3
687 Views
Last Modified: 2012-06-27
I want to find some set of words should be find out in a directory. In that directory sub folders are there.In that sub folders it should search only in java files.detaills should be captured in such a way that In which file the particular word is present , line number and the entire line where the word is present.

for example some set of words are there
String arr[]={"WARNING","ERROR"};
program should search all the sub folders whenever it is matched above words ,it should capture line number,entire line and the filename.
0
Comment
Question by:chaitu chaitu
3 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 21775232
Use File.listFiles() to list out contents of a folder. http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#listFiles()
 
Then, use LineNumberReader http://java.sun.com/j2se/1.5.0/docs/api/java/io/LineNumberReader.html
to read line one by one and check if the keyword is present. You can call http://java.sun.com/j2se/1.5.0/docs/api/java/io/LineNumberReader.html#getLineNumber() to find out
current line number.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21776518
Sounds like you should just use findstr or grep

http://www.computerhope.com/findstr.htm
0
 
LVL 2

Accepted Solution

by:
themuppeteer earned 500 total points
ID: 21777213
...and on File you have the isDirectory method to check if you should search deeper.

searching files could be done like in the snippet.


Best regards,
themuppeteer
http://users.telenet.be/free_quiz



public void searchFiles(List result, File startDirectory) {

		if (startDirectory.isDirectory()) {

			File[] r = startDirectory.listFiles();

			for (File f : r) {

				// its a file, add it

				if (f.isFile())

					result.add(f);

				// its a directory, search deeper

				if (f.isDirectory())

					searchFiles(result, f);

			}

		}

	}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now