import java.util.*;  // to use Vector
import java.io.*;    // to use IO classes

/** A program that demonstrates the use of various file IO classes
  and methods to read and write character-based text file, and 
  demonstrates the use of the Vector class
*/
public class TestFileIO {
  public static void main(String[] args) {

    String line;  // the next input line
    Vector vector = new Vector();  // create a Vector object

    try {
        /* open a character-based input stream to read file using the
           local encoding; it genrates FileNotFoundException if the
           input file doesn't exist */
        BufferedReader in = new BufferedReader (
                 new FileReader("input.txt"));

        // read one line per iteration until end of file
        while ((line = in.readLine()) != null)
          vector.add(line);  // append String object to a Vector

        // output the vector's size to screen
        System.out.println("vector's size = " + vector.size());

        /* create a character-based output file; it generates
           IOException if failed (e.g., no write privilege) */
        PrintWriter out = new PrintWriter(
                          new BufferedWriter(
                          new FileWriter("output.txt")));

        // output all Strings objects to output file, one per line
        for (int j = 0; j < vector.size(); j++)
          out.println(vector.get(j));

        out.close();  // close output file
    } catch (FileNotFoundException e) {
        System.out.println("FileNotFoundException occurs");
        System.exit (-1);
    } catch (IOException e) {  // catch other types of IOException
        System.out.println("IOException occurs");
        System.exit (-1);
    }
  }  // end of main()
}
