Solved

how find specific words in a file

Posted on 2008-06-12
3
686 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

747 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

9 Experts available now in Live!

Get 1:1 Help Now