CSC 8310 -- Simple Input-Output

David Matuszek,   dave@acm.org
Fall 1998, Villanova University

The following program uses simple line-oriented input-output to copy the contents of one file to another file.

import java.io.*;

public class CopyFile
{
  public static void main (String args [])
  {
    BufferedReader in;
    BufferedWriter out;
    String oneLine;
    
    if (args.length != 2)  // Do some minimal error checking
      {
        System.err.println ("Usage: java CopyFile <inputfile> <outputfile>");
        System.exit (1);
      }
    try
      {
        in  = new BufferedReader (new FileReader (args[0]));
        out = new BufferedWriter (new FileWriter (args[1]));
        while ((oneLine = in.readLine ()) != null)
          {
            out.write (oneLine);
            out.newLine ();
          }
        out.flush ();
        in.close ();
        out.close ();
      }
    catch (IOException e)
      {
        System.err.println ("*** Error *** " + e);
      }
  }
}

Notice the following: