Implementing unix "grep" command in java

I have a request from my user to list of the files from the folders
which has got the string he is searching for. How do I implement the
unix command grep in JAVA. or are there any methods/interfaces in java
that can scan file(s) in a given folder & do the same functionality?

Advertisements

nospam wrote:
> Hi..All,
>
> I have a request from my user to list of the files from the folders
> which has got the string he is searching for. How do I implement the
> unix command grep in JAVA. or are there any methods/interfaces in java
> that can scan file(s) in a given folder & do the same functionality?

You have the machinery of java.io.File to locate the
files in a directory, and other machinery in java.io to
open them and read their contents. The java.util.regex
package provides classes and methods that should be helpful
in detecting the presence of the desired strings -- or if
they're really just strings and not regular expressions,
you might simply use the indexOf(String) method of String.

Advertisements

On 15-4-2005 17:33, nospam wrote:
> Hi..All,
>
> I have a request from my user to list of the files from the folders
> which has got the string he is searching for. How do I implement the
> unix command grep in JAVA. or are there any methods/interfaces in java
> that can scan file(s) in a given folder & do the same functionality?
>
> Regards,
>
> P
Grep.java at
<http://java.sun.com/j2se/1.5.0/docs/guide/nio/example/index.html>
Don't know how it compares to Unix' grep.
--
Regards,

nospam wrote:
> I have a request from my user to list of the files from the
folders
> which has got the string he is searching for. How do I implement the
> unix command grep in JAVA. or are there any methods/interfaces in
java
> that can scan file(s) in a given folder & do the same functionality?

It has a main() to run it from the command line,
but all the functionality is encapsulated in
a class too. The concept of `line' is, however,
replaced by the concept of `region of interest',
which makes it a nice tool for XML grepping.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!