Chapter 19 - Binary I/O Flashcards
What is a text file, and what is a binary file?
A text file is a file that can be processed (read, created, or modified) using a text editor such as Notepad on Windows.
All other files (files that are not text files) are binary files. You cannot read binary files using a text editor - they are designed to be read by programs.
Although it is not technically precise and correct, you can envision a text file as consisting of a sequence of characters and a binary file as consisting of a sequence of bits.
How do you write or read text data to/from a file?
Using a File object that encapsulates the properties of a file or a path, you use the PrintWriter class to write to a file: PrintWriter output = new PrintWriter(file); output.print("noodles"); And use the Scanner class to read from a file: Scanner input = new Scanner(file); String str = input.nextLine();
An input object is also called ____.
An output object is also called ____
An input stream
An output stream
What is a stream?
A stream is an object that connects a program to a file
What is the difference between text I/O and binary I/O?
Binary I/O is more efficient because binary I/O does not require encoding and decoding.
Describe the classes java.io.InputStream and java.io.OutputStream
InputStream and OutputStream are abstract classes. They are the root classes for reading and writing binary data.
The java.io.InputStream class has one abstract method (and several concrete ones). What is the abstract method? The same is true for java.io.OutputStream. What is OutputStream's abstract method?
java. io.InputStream’s abstract method is ‘public int read()’. It is meant to read and return the next byte of data from the input stream. The value is returned as an int value in the range 0 to 255. If the end of the stream is reached, it returns -1.
java. io.OutputStream’s abstract method is ‘public void write(int b)’. It writes the specified byte to the output stream. The parameter b is an int value.
What classes do we use for reading/writing bytes from/to files?
We use FileInputStream and FileOutputStream. All the methods in these classes are inherited from InputStream and OutputStream. FileInputStream/FileOutputStream does not introduce new methods.
How do you create a java.io.FileInputStream?
java.io.FileInputStream has two constructors:
+FileInputStream(file: File);
+FileInputStream(filename: String)
Example:
FileInputStream input =
new FileInputStream(“filename.binary”);
How do you create a java.io.FileOutputStream?
java.io.FileOutputStream has four constructors:
+FileOutputStream(file: File);
+FileOutputStream(filename: String);
+FileOutputStream(file, File, append: boolean);
+FileOutputStream(filename: String, append: boolean);
The first two constructors will delete the current content of the file, while the last two constructors will keep the content and append new data to the end of the file, if the second parameter given is ‘true’.
What exception may the I/O classes throw?
They throw java.io.IOException. Therefore, you have to declare java.io.IOException to throw in the method or place the code in a try-catch block.
What is important when finished writing to a file?
Always close it:
output.close();
Can you use instances of FileInputStream/FileOutputStream to construct a Scanner/PrintWriter?
Yes.
PrintWriter output =
new PrintWriter(new FileOutputStream(“temp.txt”, true));
This will create an output stream to the file temp.txt, and will keep any data present, and append to the end of the file.
What is a filter stream?
Filter streams are streams that filter bytes for some purpose. The basic byte input stream provides a read method that can be used only for reading bytes. If you want to read integers, doubles, or strings, you need a filter class to wrap the byte input stream.
What are the base classes for filtering data streams called?
FilterInputStream and FilterOutputStream.
Describe DataInputStream DataOutputStream.
DataInputStream reads bytes from the stream and converts them into appropriate primitive type values or strings.
DataOutputStream converts primitive type values or strings into bytes and outputs the bytes to the stream.
They extend FilterInputStream / FilterOutputStream, and implements the DataInput/DataOutput interfaces.
They read and write Java primitive type values and strings in a machine-independent fashion, thereby enabling you to write a data file on one machine and read it on another machine that has a different operating system or file structure.
What are the methods contained in DataInputStream and DataOutputStream?
They contain methods for reading and writing different primitive types:
+readBoolean(): boolean
+writeBoolean(b: boolean): void
and similar methods for reading/writing byte, char, float, double, int long, short,
How do you construct a DataInputStream / DataOutputStream?
The constructors: public DataInputStream(InputStream instream) public DataOutputStream(OutputStream outstream)
Example: DataInputStream input = new DataInputStream(new FileInputStream("in.binary")); DataOutputStream output = new DataOutputStream(new FileOutputStream("out.binary"));
If you keep reading data at the end of an InputStream, what will happen?
An EOFException will occur. This exception can be used to detect the end of a file.
How is BufferedInputStream / BufferedOutputStream different from DataInputStream / DataOutputStream?
They are exactly the same – they contain the same methods – except that BufferedInputStream / BufferedOutputStream manages a buffer behind the scene. They speed up input and output by reducing the number of disk reads and writes.
Using BufferedInputStream, the whole block of data on the disk is read into the buffer in the memory once. The individual data are then delivered to your program from the buffer. Using BufferedOutputStream, the individual data are first written to the buffer in the memory. When the buffer is full, all data in the buffer is written to the disk once.
What is the default size of the buffer (BufferedInputStream)?
The default size is 512 bytes.
When should you use buffered I/O?
You should (nearly) always use buffered I/O to speed up input and output. For small files, you may not notice performance improvements. However, for large files – over 100 MB – you will see substantial improvements using buffered I/O.
What byte value signifies the end of a file?
-1
Describe ObjectInputStream / ObjectOutputStream.
ObjectInputStream / ObjectOutputStream contains all the functions of DataInputStream / DataOutputStream, but also enables you to perform I/O for objects.
When using ObjectInputStream, remember that it may throw a ClassNotFoundException. Also, when reading objects from an input stream, you must type-cast it back to the original type.