13 November 2020


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

public class Grip
{
    public static void main(String[] args)
    {
        if(args.length < 2)
        {
            System.err.println("Specify a search string then a file.");
            System.exit(1);
        }
        
        String fileName = args[1];
        File f = new File(fileName);
        if(!f.exists())
        {
            System.err.println("You done sent me on a goose chase!");
            System.err.printf("File %s does not exist!\n", 
                f.getAbsolutePath());
            System.exit(1);
        }
        String searchString = args[0];
        try
        {
            BufferedReader br = new BufferedReader(
                new FileReader(fileName));
            String line = "";
            while( br.????())
            {
                if(line.contains(searchString))
                {
                System.out.print(???);
                }
            }
        }
        catch(FileNotFoundException ex)
        {
            System.err.printf("File %s does not exist.\n", 
                fileName);
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }
}

A Closer look at the File Class

This class has many useful methods for interacting with your file system. We are going to explore it and check some thihgs out.