Solved

how find specific words in a file

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

861 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