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.