?
Solved

how find specific words in a file

Posted on 2008-06-12
3
Medium Priority
?
692 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month7 days, 22 hours left to enroll

765 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