Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.8. Java code exercises  >   5.8.10. Code exercise 10

5.8.10. Code exercise 10
Last updated: 27 January 2013.

The idea is to write a program that searches for a given string in a list of text files. The string to search for and the list of files are command-line arguments. The first command-line argument is the string to search for, followed by the list of files. In that list, if a file does not exist, it is ignored by the program.

You can create two classes: a class named App that contains the main method, and a class named Practice containing a method named searchInFiles(String[] args):

public final class App {

   
public static void main(String[] args) {
         
new Practice().searchInFiles(args);
   
}

}

Here is the class Practice:

public final class Practice {

   
public void searchInFiles(String[] args){
         
// the code goes here
   
}

}

When the program finds a line of text containing the searched string in a file, it prints to the standard output the name of the file followed by a colon, followed by the entire line. For example, if the user searches for the string "public" in the files Practice.java and App.java by typing the following command:

java App public App.java Practice.java

The output must be the following:

App.java:public class App {
App.java:       public static void main(String[] args) {
Practice.java:public class Practice {
Practice.java:       public void searchInFiles(String[] args){



import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public final class Practice {

   
private StringBuffer stringBuffer = new StringBuffer();

   
public void searchInFiles(String[] args){

         
if(args.length < 2){
               
System.out.println("The program requires at least 2 command-line arguments");
               
return;
         
}

         
for(int i=1; i<args.length; ++i){
               
File f = new File(args[i]);
               
if(f.exists() && f.isFile() && f.canRead())
                     
searchInSingleFile(args[0], f);
         
}
    }


   
private void searchInSingleFile(String searchString, File file){

         
BufferedReader reader = null;
          String fileName = file.getName
();

         
try{
               
reader = new BufferedReader(new FileReader(file));

                String line = reader.readLine
();

               
while(line != null){
                     
if(line.contains(searchString)){
                           
stringBuffer.delete(0, stringBuffer.length());
                            stringBuffer.append
(fileName).append(":").append(line);
                            System.out.println
(stringBuffer);
                     
}

                     
line = reader.readLine();
               
}
          }
         
catch(IOException e){
               
e.printStackTrace();
         
}
         
finally{
               
if(reader != null){
                     
try{
                           
reader.close();
                     
}
                     
catch(IOException e){
                           
e.printStackTrace();
                     
}
                }
          }
    }

}

Note that on Linux and Mac systems, the grep command fills the same role as the program shown above. Linux and Mac users can get the same result by typing the following command in a terminal:

grep public App.java Practice.java


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.8. Java code exercises  >   5.8.10. Code exercise 10
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7