Input

This application illustrates the use of the BufferedReader class for writing a stream of character data to a file. It also illustrates the rather extensive use of chaining that is typical of Java I/O.

In this example, four classes are chained. You should look closely at their APIs.

BufferedReader
InputStreamReader
FileInputStream
File
import java.awt.*;
import java.io.*;

public class Input  {

//  simplest read from a file

  public Input  ()  {
    super ();
  }  // end FileStreamRead  constructor

  public static void main ( String [ ] args )  {

  File inFile;
  FileInputStream inFileStream;
  InputStreamReader inStreamReader;
  BufferedReader  inBufReader;

  String fileName;
  String path;

  String line;

  Input readAppl = new Input ();

  // Instantiate the file
  if ( args.length > 0 ) fileName= args[0];
  else {
    fileName = new String ( "testfile" );
    path = new String ( "../testdata" );
  }
  if ( args.length > 1 ) path = args[1];
  else path = new String ( "../testdata" );

  inFile = new File ( path, fileName );

  // Instantiate and chain the FileInputStream
  try {
    inFileStream = new FileInputStream ( inFile );
    }  // end try
  catch ( IOException except ) {
    return;
    }  // end catch

  // Instantiate and chain the InputStreamReader
  inStreamReader = new InputStreamReader ( inFileStream );

  // Instantiate and chain the BufferedReader
  inBufReader = new BufferedReader ( inStreamReader );
  System.out.println ( "Reading from file: " + inFile.getName() );

  boolean more = true;
  try {
    while ( more ) {
        line = inBufReader.readLine ( );
        if (line == null ) more = false;
        else System.out.println ( line );
    }  // end while
    }
  catch ( IOException except ) {;}
    // end of read

  try { inBufReader.close (); } catch ( IOException except ) { ; }

  }  // end main

}  // end Input