Computer Programming I

COP 2210

Working with Data Files in Java

  1. Concepts and Terms
  • A file is an organized collection of information stored on a disk.
  • Files may contain anything - documents, spreadsheets, graphics, programs, data, etc.
  • Input files contain data to be read by a program.
  • Output files contain the results (output) of a program.
  • During the life of a file it may be both an output file and an input file, but not at the same time. For example, an output file created by one program could later be used as input to another program.
  • Files are either binary files or text files.

Binary files contain specially coded information that can only be read by the program that produced it, or by some other special program. Some examples of binary files include music, videos, pictures, sounds, executable programs, spreadsheets, and word-processing documents

Text files contain only ASCII characters(or Unicode characters) and are therefore "generic," and can be read by any program (Word, WordPad, NotePad, NetBeans, etc). Examples of text files include Java source code (the .java files but not the compiled .class files), .txt files produced by NotePad, and documents produced by word processing programs but saved as "plain text."

  • Files are either sequential access or random access.

Sequential access means that the file must be read/written in order from the beginning to the end. The only way to get to, say, the fifth line (or, "record") of the file is to read/write lines one through four first. Think of a cassette tape.

Random access means that any record of the file may be directly read/written at any time, regardless of where it is located in the file. Think of a CD (compact disk).

This document explains how to work with sequential text files in Java.

  1. Imports and "Throws Clauses"
  • To work with data files, we need to import several classes that are in Java's "io" package:IOException, and PrintWriter (for output files) and File (for input files to be read by Scanner)
  • When working with data files, exceptions may be thrown. The Java compiler requires you to acknowledge this in either of two ways:
  1. provide an exception handler (a special block of code that is executed when an exception occurs), or
  1. tell the compiler that you are aware of the possibility of an exception and take responsibility should one occur, although you do not wish to "handle" it. This is done by appending a "throws clause" to the heading of a method that may throw an exception. E.g.,

public static void main(String[] args) throws IOException

For now, we will use the throws clause. Exception-handling will be covered in Programming II.

  1. Reading Data from Input Files using Scanner

Our old friend the Scanner class makes it very easy to work with input files.

Instead of creating a Scanner object associated with a String objector withSystem.in, we simply create one associated with a File object. The syntax is:

Scannername = newScanner(new File(filename) ) ;

Here are some examples:

Scanner reader = new Scanner( new File (“C:\\Documents and ” +

“Settings\\Greg\\Desktop\\2210\\CD-data.txt”) ) ;

Scanner readFile = new Scanner( new File (“E:Grades.txt”) ) ;

Scanner fileIn = new Scanner( new File ( “Votes.txt”) ) ;

  • In the first example, Scanner object reader is associated with file CD-Data.txt, located in folder 2210 on the Windows XP Desktop.

Note that two backslashes are used instead of one (one backslash is how Windows indicates a folder). This is because the Java compiler interprets the backslash as the escape character. In Java, "\\" is the escape sequence for the backslash!

  • In the second example, Scanner object readFile is associated with file Grades.txt, located in the root directory of E:
  • In the last example, no path is provided for the file Votes.txt. In that case, the file must reside in your NetBeans project folder and not in the src folder or any other subfolder.

If the specified file does not exist in the specified folder, or the folder does not exist in the specified path, a FileNotFoundException is thrown!

After creating the Scanner object, we read data from the file using the familiar methods hasNext(),next(), nextInt(), nextDouble(), andnextLine(), (See “Intro to the Scanner Class”, online, and InputDemo2.java andScannerDemo.java)

IV. Checking for “end of file” – the hasNext() Method

The Scanner class has a boolean method called hasNext() that returns true if there remains at least one non-whitespace character in the file that has not yet been extracted. Otherwise, if all non-whitespace char’s have been extracted (i.e., we have reached the end-of-file), it returns false.

So, assuming a Scanner object called scan has been created and associated with a file, we can read and process all the data in the file using a while loop:

while ( scan.hasNext() )// while not eof

{

// extract data

// process data

}

  1. The PrintWriter Class (for output files)

You tell Java that you wish to write data to a particular output file by creating a PrintWriter object associated with that file.

The syntax is:

PrintWriter object-var = new PrintWriter(filename) ;

where filename can be either a File object or a string

Here are two examples:

  1. PrintWriter outFile = new PrintWriter("Output.txt") ;
  1. File outputFile = new File("results.txt") ;

PrintWriter out = new PrintWriter(outputFile) ;

Note that the two statements in the second example can be combined into in a single statement, like this:

PrintWriter out = new PrintWriter(new File("results.txt")) ;

Naturally, the rules regarding the file path are the same as for input files (see III., above), with one important difference:

If an output file does not exist, it is automatically created.If it does exist, it is destroyed and re-created.

The PrintWriter class "overrides" methods print(), println(), and printf() so that writing to a file is essentially the same as writing to the screen. Just call those methods for the PrintWriter object – in the examples above, outFile and out -instead of for System.out.

(Method overridingwill be covered in ProgrammingII)

  1. Closing a File

When done with afile, you must "tie up loose ends" relating to the file by calling the close method for your Scanner or PrintWriter object. E.g., for the first PrintWriter object created above:

outfile.close() ;

Forgetting to close an output file will cause the data to be lost!

  1. Examples
  • For examples of programs that read from input files, see Magic8BallTester.java, and BankTester2.java
  • For output files, see Bank2.java

Material originally created by Greg Shaw, modified by Caryl Rahn.