Download Working with Data Files in Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Programming I
COP 2210
Working with Data Files in Java
I.
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).
Material originally created by Greg Shaw, modified by Caryl Rahn.

This document explains how to work with sequential text files
in Java.
II. 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
2.
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

III.
For now, we will use the throws clause. Exception-handling
will be covered in Programming II.
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 object
or with System.in, we simply create one associated with a File
object. The syntax is:
Scanner name = new Scanner( 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
Material originally created by Greg Shaw, modified by Caryl Rahn.
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(), and nextLine(), (See “Intro to the Scanner
Class”, online, and InputDemo2.java and ScannerDemo.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 nonwhitespace 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() )
{
// extract data
// process data
}
// while not eof
IV. 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") ;
Material originally created by Greg Shaw, modified by Caryl Rahn.
2.
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 overriding will be covered in Programming II)
V. Closing a File
When done with a file, 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!
VI. Examples

For examples of programs that read from
Magic8BallTester.java, and BankTester2.java

For output files, see Bank2.java
Material originally created by Greg Shaw, modified by Caryl Rahn.
input
files,
see