A file is a collection of data stored on a disk. A stream is an object that delivers data to and from other objects.
An InputStream is a stream that delivers data to a program from an external source—such as the keyboard or a file. System.in is an example of an InputStream. An OutputStream is a stream that delivers data from a program to an external destination—such as the screen or a file. System.out is an example of an OutputStream.
Data can be viewed as a hierarchy. From highest to lowest, a database is a collection of files. A file is a collection of records. A record is a collection of fields. A field is a collection of bytes. A byte is a collection of 8 bits. A bit is one binary digit, either 0 or 1.
A binary file is a sequence of 0s and 1s that is interpreted as a sequence of bytes. A text file is a sequence of 0s and 1s that is interpreted as a sequence of characters. A text file can be read by any text editor, but a binary file cannot. InputStream and OutputStream are abstract classes that serve as the root classes for reading and writing binary data. Reader and Writer serve as root classes for text I/O. Buffering is a technique in which a buffer, a temporary region of memory, is used to store data while they are being input or output.
A text file contains a sequence of characters divided into lines by the \n character and ending with a special end-of-file character.
The standard algorithm for performing I/O on a file consists of three steps: (1) Open a stream to the file, (2) perform the I/O, and (3) close the stream.
Designing effective I/O routines answers two questions: (1) What streams should I use to perform the I/O? (2) What methods should I use to do the reading or writing?
To prevent damage to files when a program terminates abnormally, streams should be closed when they are no longer needed.
Most I/O operations generate an IOException that should be caught in the I/O methods.
Text input uses a different technique to determine when the end of a file has been reached. Text input methods return null or -1 when they attempt to read the special end-of-file character. Binary files don’t contain an end-of-file character, so binary read methods throw an EOFException when they attempt to read past the end of the file.
The java.io.File class provides methods that enable a program to interact with a file system. Its methods can be used to check a file’s attributes, including its name, directory, and path.
Streams can be joined together to perform I/O. For example, a DataOutputStream and a FileOutputStream can be joined to perform output to a binary file.
A binary file is “raw” in the sense that it contains no markers within it that allow you to tell where one data element ends and another begins. The interpretation of binary data is up to the program that reads or writes the file.
Object serialization is the process of writing an object to an output stream. Object deserialization is the reverse process of reading a serialized object from an input stream. These processes use the java.io.\-ObjectOutputStream and java.io.ObjectInputStream classes.
The JFileChooser class provides a dialog box that enables the user to select a file and directory when opening or saving a file.
Solutions11.9.3Solutions to Self-Study Exercises
11.3CASE STUDY: Reading and Writing Text Files 11.3.4Code Reuse: Designing Text File Output
Self-Study Exercise
11.3.4.1.PrintWriter.
Solution.
Because FileWriter contains a constructor that takes a file name argument, FileWriter(String), it can be used with PrintWriter to perform output to a text file:
PrintWriter outStream = // Create output stream
new PrintWriter(new FileWriter(fileName)); // Open file
outStream.print (display.getText()); // Print the text
outStream.close(); // Close output stream
11.3.5Reading from a Text File
Self-Study Exercises
11.3.5.1.Read Loop.
Solution.
String line = inStream.readLine(); // Read one line
while (line != null) // While more text
{
display.append(line + "\n"); // Display a line
line = inStream.readLine(); // Read next line
}
11.3.6Code Reuse: Designing Text File Input
Self-Study Exercise
11.3.6.1.Read Loop Characters.
Solution.
int ch = inStream.read(); // Init: Try to read a character
while (ch != -1)
{ // Entry-condition: while more chars
display.append((char)ch + ""); // Append the character
ch = inStream.read(); // Updater: try to read
}
11.4The FileClass 11.4.2Validating File Names
Self-Study Exercise
11.4.2.1.File Info.
Solution.
public void getFileAttributes(String fileName) {
File file = new File (fileName);
System.out.println(filename);
System.out.println("absolute path:"
+ file.getAbsolutePath());
System.out.println("length:" + file.length());
if (file.isDirectory())
System.out.println("Directory");
else
System.out.println("Not a Directory");
} // getFileAttributes()
11.5Example: Reading and Writing Binary Files 11.5.2Reading Binary Data
Self-Study Exercise
11.5.2.1.Find the error.
Solution.
The inStream.close() statement should go in the finally block:
public void readIntegers(DataInputStream inStream) {
try {
while (true) {
int num = inStream.readInt();
System.out.println(num);
}
} catch (EOFException e) {
} catch (IOException e) {
} finally {
inStream.close();
}
} // readIntegers
11.6Object Serialization: Reading and Writing Objects 11.6.2The ObjectIOClass
Self-Study Exercise
11.6.2.1.Serializable?
Solution.
Yes, a binary file containing several SomeObjects would be “readable” by the BinaryIO program because the program will read a String followed by 64 bytes. However, BinaryIO would misinterpret the data, because it will assume that n1 and n2 together comprise a single int, and n3(64 bits) will be interpreted as a double. A file of SomeObjects could not be read by the ObjectIO program, because SomeObject does not implement the Serializable interface.