Transcript ch12
Chapter 12 Using data files
The concept of file
Logically cohesive data stored on a permanent storage,
such as hard disk (most cases), CD, USB.
Types of data files
• object (.obj)
• executable (.exe)
• document (.doc)
• text (.txt): A sequence of characters, including special
characters such as line break, end of file. A (Java)
source code can be viewed as a text file.
Text files vs. strings
•
Although text files and strings both contain character data, it
is important to keep in mind the following differences:
1.
2.
The information stored in a file is permanent. The value of a string
variable persists only as long as the variable does. Local variables
disappear when the method returns, and instance variables disappear
when the object goes away. Information stored in a file exists until
the file is deleted.
Files are usually read/written sequentially. When you read/write data
from/to a file, you usually start at the beginning and read/write the
characters in order.
Reading a text file
• Include Java.io package
import java.io.*;
• Open a file by constructing an object that is
ultimately an instance of the FileReader class
FileReader rd = new FileReader(“Hamlet.txt”);
Object rd is associated with the file Hamlet.txt.
The FileReader class offers a low-level set of methods that
supports reading characters from the file one at a time.
(inconvenient)
Reading a text file (cont.)
• Construct a higher level class from any reader.
Usually you call both constructors in the same
declaration.
BufferedReader rd = new BufferedReader (new FileReader(“Hamlet.txt”));
• Read a line into a buffer (string variable)
String line = rd.readLine();
A file is accessed sequentially. After a read, the current
position is updated. The next read/write starts from the new
position.
• Close the file. Finish reading.
rd.close();
Writing a text file
• Include Java.io package
import java.io.*;
• Open a file for writing
PrintWriter wr = = new PrintWriter(new FileWriter(“Hello.txt”));
Object wr is associated with the file Hello.txt.
• Write a line to the file
wr.println(“Hello, world”);
A file is accessed sequentially. After a write, the current position is
updated. Next read/write starts from the new position.
• Close the file. Finish writing.
wr.close();
Exception handling
• Unfortunately, the process of reading a file is not as simple as
the previous slides suggest. When you work with the classes in
the java.io package, you must ordinarily indicate what to do if
an operation fails. In the case of opening a file, for example,
you need to specify what the program should do if the
requested file does not exist.
• Java’s library classes often respond to such conditions by
throwing an exception. If the FileReader constructor, for
example, cannot find the requested file, it throws an
IOException to signal that fact.
• When Java throws an exception, it stops whatever it is doing
and looks back through its execution history if any method has
indicated an interest in “catching” that exception by including
a try statement.
The try statement
• Java uses the try statement to indicate an interest in catching an exception.
In its simplest form, the try statement syntax is
try {
code in which an exception might occur
} catch (type identifier) {
code to respond to the exception
}
where type is the name of some exception class and identifier is the name of a
variable used to hold the exception itself.
• The range of statements in which the exception can be caught includes not
only the statements explicitly enclosed in the try body but also any methods
those statements call. If an exception occurs inside some other method, any
subsequent stack frames are removed until control returns to the try
statement itself.
The End