By Barry Burd

Java gives you the capability of writing a disk-oriented program. In fact, it is sometimes easier to use some of your pre-existing code and add some simple editing. Here’s how:

  • Add the following import declarations to the beginning of your code:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
  • Add the following throws clause to the method header:

    throws FileNotFoundException
  • In the call to new Scanner, replace System.in with a call to new File as follows:

    Scanner aVariableName =
     new Scanner(new File("inputFileName"))
  • Create a PrintStream for writing output to a disk file:

    PrintStream anotherVariableName =
     new PrintStream("outputFileName");
  • Use the Scanner variable name in calls to nextInt,nextLine, and so on.

    For example, you would change

    unitPrice = keyboard.nextDouble();
    quantity = keyboard.nextInt();

    to

    unitPrice = diskScanner.nextDouble();
    quantity = diskScanner.nextInt();
  • Use the PrintStream variable name in calls to print and println.

    For example, you would change

    System.out.println(total);

    to

    diskWriter.println(total);
  • Use the Scanner variable name in the call to close.

    For example, you would change

    keyboard.close();

    to

    diskScanner.close();
  • Use the PrintStream variable name in a call to close.

    For example, you would add

    diskWriter.close();

    at the end of the main method.

Reading from a file

All the Scanner methods can read from existing disk files. For example, to read a word from a file named mySpeech, use code of the following kind:

Scanner diskScanner =
 new Scanner(new File("mySpeech"));
String oneWord = diskScanner.next();

To read a character from a file named letters.dat and then display the character on the screen, you can do something like this:

Scanner diskScanner =
 new Scanner(new File("letters.dat"));
System.out.println(
 diskScanner.findWithinHorizon(".",0).charAt(0));

The file you need to read from is named mySpeech, not mySpeech.txt or mySpeech.doc. Anything that you put after the dot is called a filename extension, and for a file full of numbers and other data, the filename extension is optional.

Sure, a Java program must be called something.java, but a data file can be named mySpeech.txt, mySpeech.reallymine.allmine, or just mySpeech. As long as the name in your new File call is the same as the filename on your computer’s hard drive, everything is okay.

Writing to a file

The print and println methods can write to disk files. Here are some examples:

  • During a run of the code in Listing 13-2, the variable total stores the number 99.75. To deposit 99.75 into the cookedData.txt file, you execute

    diskWriter.println(total);

    This println call writes to a disk file because of the following line in Listing 13-2:

    PrintStream diskWriter =
     new PrintStream("cookedData.txt");
  • In another version of the program, you may decide not to use a total variable. To write 99.75 to the cookedData.txt file, you can call

    diskWriter.println(unitPrice * quantity);
  • To display OK on the screen, you can make the following method call:

    System.out.print("OK");

    To write OK to a file named approval.txt, you can use the following code:

    PrintStream diskWriter =
     new PrintStream("approval.txt");
    diskWriter.print("OK");
  • You may decide to write OK as two separate characters. To write to the screen, you can make the following calls:

    System.out.print('O');
    System.out.print('K');

    And to write OK to the approval.txt file, you can use the following code:

    PrintStream diskWriter =
     new PrintStream("approval.txt");
    diskWriter.print('O');
    diskWriter.print('K');
  • Like their counterparts for System.out, the disk-writing print and println methods differ in their end-of-line behaviors. For example, you want to display the following text on the screen:

    Hankees Socks
    7  3

    To do this, you can make the following method calls:

    System.out.print("Hankees ");
    System.out.println("Socks");
    System.out.print(7);
    System.out.print("  ");
    System.out.println(3);

    To plant the same text into a file named scores.dat, you can use the following code:

PrintStream diskWriter =
 new PrintStream("scores.dat");
diskWriter.print("Hankees ");
diskWriter.println("Socks");
diskWriter.print(7);
diskWriter.print("  ");
diskWriter.println(3);