/Users/petercappello/NetBeansProjects/56-2014/56-2014-5-Gala/src/TextFileReader.java

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFileChooser;

/**
 * Read a text file.
 * @author Peter Cappello
 */
abstract public class TextFileReader 
{            
    /**
     * If file is successfully chosen, this is the time to process it.
     * Else, it is the time taken to unsuccessfully select the file.
     */
    private long processingTime;
    private File readFile;
    
    TextFileReader()
    {
        long startTime = System.currentTimeMillis();
        JFileChooser fileChooser = new JFileChooser();
        int returnValue = fileChooser.showOpenDialog( null );
        if ( returnValue == JFileChooser.APPROVE_OPTION )
        {
            readFile = fileChooser.getSelectedFile();
        }
        processingTime = System.currentTimeMillis() - startTime;
    }
    
    public boolean hasFile() { return readFile != null; }
    
    /**
     * Process the input file, 1 line at a time.
     * We equally well could use a BufferedReader instead of a Scanner.
     * @throws IOException when closing the file.
     */
    public void process() throws IOException
    {
        long startTime = System.currentTimeMillis(); 
        try ( Scanner scanner =  new Scanner( readFile ) )
        {
            while ( scanner.hasNextLine() )
            {
                processLine( scanner.nextLine() );
            }
        }
        processingTime = System.currentTimeMillis() - startTime;
    }
            
    abstract public void processLine( String line );
    
    public long getProcessingTime() { return processingTime; }
}