Class TextFileRead

java.lang.Object
  |
  +--TextFileRead

public class TextFileRead
extends java.lang.Object

This example program reads lines of text from a file and writes them on the console window.


Constructor Summary
TextFileRead(java.lang.String fn)
          Create a new TextFileRead object with which to read a file.
 
Method Summary
 void close()
          Close the stream.
 java.lang.String getNextLine()
          Read one line from the text file and return it as a String to the caller.
 java.lang.String getNextNonBlankLine()
          Read one line from the text file and return it as a String to the caller.
static void main(java.lang.String[] arg)
          Test harness for this class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TextFileRead

public TextFileRead(java.lang.String fn)
             throws java.io.IOException
Create a new TextFileRead object with which to read a file. The code has two ways of doing this, so one of them is commented out.

Parameters:
fn - the name of the file to read.
Throws:
java.io.IOException - if there is an error opening the file
Method Detail

getNextLine

public java.lang.String getNextLine()
                             throws java.io.IOException
Read one line from the text file and return it as a String to the caller. The line might be null (at end of file), empty (0 characters), or blank (all whitespace). Of course, it might also be a non-blank String with some useful characters in it.

Returns:
a String containing the next line or null if we are at the end of the file
Throws:
java.io.IOException - if there is an error reading the file

getNextNonBlankLine

public java.lang.String getNextNonBlankLine()
                                     throws java.io.IOException
Read one line from the text file and return it as a String to the caller. Skip lines that are empty (0 characters) or that are blank (all whitespace).

Returns:
a String containing the next non-blank line or null if we are at the end of the file
Throws:
java.io.IOException - if there is an error reading the file

close

public void close()
           throws java.io.IOException
Close the stream.

Throws:
java.io.IOException - if there is an error closing the file

main

public static void main(java.lang.String[] arg)
                 throws java.io.IOException
Test harness for this class.

Parameters:
arg - arg[0] name of the file to read. Defaults to sampleIn.txt if not specified.
Throws:
java.io.IOException - if there is an error performing any I/O