/*
 * Files.java by Richard J. Davies
 * from `Introductory Java for Scientists and Engineers'
 * chapter: `The Standard Libraries'
 * section: `File Input and Output'
 *
 * This program is intended to demonstrate the Java Standard Library commands
 * for reading and writing files. It performs no useful computations.
 */
import java.io.*;

public class Files
{
  // Note that the method must throw the two exceptions
  // if it does not handle them.
  public static void main(String[] argv) throws IOException,
                                         FileNotFoundException
  {
    // Open an output file using methods supported by JDK 1.1
    // or later.
    PrintWriter q = new PrintWriter(
                     new FileOutputStream("file2.out"), true);
    // Write one line of text to it
    q.println("Hello there file2.out");

    // Open an output file the JDK 1.0 way.
    PrintStream p = new PrintStream(
                     new FileOutputStream("file1.out"));
    // Write one line of text to it
    p.println("Hello there file1.out");

    // Open an input file using methods supported by JDK 1.1
    // or later.
    BufferedReader b = new BufferedReader(
        new InputStreamReader(new FileInputStream("file4.in")));

    // Read the first line and print it out
    System.out.println(b.readLine());

    // Read the second line and ignore it
    b.readLine();

    // Read the third line and convert it to a double
    // Note that this method is only supported under JDK 1.2
    // or later.
    // You must use Double.valueOf(s).doubleValue() under earlier
    // versions
    String s = b.readLine();
    double x = Double.parseDouble(s);
    System.out.println("The double is: " + x);

    // Read the fourth line and convert it to an integer
    s = b.readLine();
    int i = Integer.parseInt(s);
    System.out.println("The integer is: " + i);

    // Open an input file the JDK 1.0 way.
    DataInputStream d = new DataInputStream(
                         new FileInputStream("file3.in"));
    // Read one line of text from it
    System.out.println(d.readLine());
  }
}

