Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how find specific words in a file

Posted on 2008-06-12
3
Medium Priority
?
693 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 video teaches viewers about errors in exception handling.
Suggested Courses

604 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