Podcast
Questions and Answers
What does the File
class encapsulate?
What does the File
class encapsulate?
The properties of a file or a path.
What type of file is designed to be handled by programs?
What type of file is designed to be handled by programs?
Binary files
Text files can be read by the JVM.
Text files can be read by the JVM.
False
Which of these classes are a subclass of OutputStream
in Java? (Select all that apply)
Which of these classes are a subclass of OutputStream
in Java? (Select all that apply)
Signup and view all the answers
What is the return type of the read()
method in InputStream
?
What is the return type of the read()
method in InputStream
?
Signup and view all the answers
What is the purpose of FilterInputStream
and FilterOutputStream
?
What is the purpose of FilterInputStream
and FilterOutputStream
?
Signup and view all the answers
It is possible to create a FileInputStream
with a non-existent file.
It is possible to create a FileInputStream
with a non-existent file.
Signup and view all the answers
What might happen if you attempt to create a FileOutputStream
with an existing file using the first two constructors?
What might happen if you attempt to create a FileOutputStream
with an existing file using the first two constructors?
Signup and view all the answers
What constructor(s) of FileOutputStream
can be used to append data to a file?
What constructor(s) of FileOutputStream
can be used to append data to a file?
Signup and view all the answers
What is the advantage of using DataInputStream
and DataOutputStream
?
What is the advantage of using DataInputStream
and DataOutputStream
?
Signup and view all the answers
What are the primary methods provided by DataInputStream
and DataOutputStream
for writing primitive data types?
What are the primary methods provided by DataInputStream
and DataOutputStream
for writing primitive data types?
Signup and view all the answers
The Serializable
interface has methods that must be implemented.
The Serializable
interface has methods that must be implemented.
Signup and view all the answers
What is the role of ObjectInputStream
and ObjectOutputStream
?
What is the role of ObjectInputStream
and ObjectOutputStream
?
Signup and view all the answers
All objects can be written to a stream.
All objects can be written to a stream.
Signup and view all the answers
What is the benefit of implementing the Serializable
interface? (Select all that apply)
What is the benefit of implementing the Serializable
interface? (Select all that apply)
Signup and view all the answers
What does the readObject
method of ObjectInputStream
do?
What does the readObject
method of ObjectInputStream
do?
Signup and view all the answers
An array is serializable if at least one of its elements are serializable.
An array is serializable if at least one of its elements are serializable.
Signup and view all the answers
What is the key difference between sequential files and random access files?
What is the key difference between sequential files and random access files?
Signup and view all the answers
The RandomAccessFile
class allows for both reading and writing to files.
The RandomAccessFile
class allows for both reading and writing to files.
Signup and view all the answers
The seek()
method in RandomAccessFile
moves the file pointer to the beginning of the file.
The seek()
method in RandomAccessFile
moves the file pointer to the beginning of the file.
Signup and view all the answers
Which of the following methods can be used in RandomAccessFile
? (Select all that apply)
Which of the following methods can be used in RandomAccessFile
? (Select all that apply)
Signup and view all the answers
Write a method call in RandomAccessFile
to create a new file that can be read from and written to.
Write a method call in RandomAccessFile
to create a new file that can be read from and written to.
Signup and view all the answers
Write a method call in RandomAccessFile
to create a new file that only allows reading.
Write a method call in RandomAccessFile
to create a new file that only allows reading.
Signup and view all the answers
Study Notes
Object-Oriented Programming SWE211
- Object-Oriented Programming course, SWE211, is presented.
Java I/O
- File objects encapsulate file properties but lack data reading/writing methods.
- Java I/O classes are needed for I/O operations.
- Scanner and PrintWriter are used to read and write files, example shown using temp.txt.
Text File vs. Binary File
- Text files store data in human-readable form.
- Binary files store data in binary format, not readable by humans.
- Java source code is in text files, while class files are binary files.
- Binary files are more efficient for processing than text files.
- Text files composed of characters, binary composed of bits. Decimal 199 is '199' in text file and C7 in binary file (decimal 199 = hex C7).
Binary I/O Classes
- Java's hierarchy of input/output classes for binary files.
InputStream / OutputStream
- Values returned/written are bytes as integers
FileInputStream/FileOutputStream
- Connects input/output streams with external binary files.
- Methods are inherited from superclasses.
FileInputStream
- Used for creating input streams to files
-
FileInputStream(String filename)
-
FileInputStream(File file)
-
FileNotFoundException
if file doesn't exist.
FileOutputStream
-
FileOutputStream(String filename)
-
FileOutputStream(File file)
-
FileOutputStream(String filename, boolean append)
-
FileOutputStream(File file, boolean append)
- Creates a new file if it doesn't exist.
- Deletes existing file content if using the first two constructors.
- Appends to existing file using the last two.
Example Code (TestFileStream)
- Demonstrates reading and writing integers to/from a binary file using
FileInputStream
andFileOutputStream
.
FilterInputStream/FilterOutputStream
- Filter streams modify basic byte input/output streams for various purposes.
-
DataInput
andDataOutput
filter bytes needed to read integers, doubles, and strings.
DataInputStream/DataOutputStream
-
DataInputStream
reads bytes from a stream and converts them into primitive data types or strings. -
DataOutputStream
converts primitive data types or strings into bytes for writing to a stream.
DataInputStream
- Implements
DataInput
interface, extendingFilterInputStream
. - Reads primitive data types from a stream.
DataOutputStream
- Implements
DataOutput
interface, extendingFilterOutputStream
. - Writes primitive data types or strings to a stream.
Using DataInputStream/DataOutputStream
- Wrappers to filter data from existing input/output streams for specific data types.
- Example using
DataInputStream
andDataOutputStream
onin.dat
andout.dat
.
Concept of Pipe Line
- Input/output can flow sequentially in a pipe-line fashion.
Order and Format
- Data must be read in the same order and format that it is written.
- Using UTF-8 example, data must be read using
readUTF
.
Checking End of File
-
EOFException
occurs if attempting to read past end of stream. -
input.available()
checks if end of file is reached.
Example Demonstrating File Handling
- Demonstrates various I/O related functions
BufferedInputStream/BufferedOutputStream
- Use buffers to improve I/O speed (memory areas to temporarily hold data)
Constructing BufferedInputStream/BufferedOutputStream
- Java methods used to create buffered input/output streams.
Object I/O
- Enables file input/output for primitive types values and strings (using
DataInputStream
/DataOutputStream
) and also for objects.
Using Object Streams
- You wrap
ObjectInputStream
orObjectOutputStream
using constructorsObjectInputStream(InputStream in)
andObjectOutputStream(OutputStream out)
Example of object handling (example program)
- Example saving object data to
foo.bin
file, demonstratesObjectOutputStream
The Serializable Interface
- Enables Java's serialization mechanism for object and array handling.
- Java will assign a serial number to each object, and use this to re-create it later.
Example Using Foo class
- Saving object
Foo
instances to the filefoo.bin
Using object output stream, writeObject(object).
Reading Objects
- Reading back instances/objects using
readObject
.
Serializing Arrays
- Entire arrays can be serialized if all elements are serializable.
Random Access Files
- Stream data that can be read and written to at random positions in a file.
RandomAccessFile Methods
- Methods (similar to
DataInputStream
andDataOutputStream
) handle random file access.
RandomAccessFile Constructor
- Demonstrates
RandomAccessFile
class constructors for "read/write" or "read only" mode.
Sample Code (RandomAccessFile)
- Demonstrates various random access file operations
- Methods to retrieve and modify data at specific byte positions.
Practice
- Exercises and coding practice
- Code presented to read/extract text & file content
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz covers Object-Oriented Programming concepts as presented in the SWE211 course. Topics include Java I/O operations, distinctions between text and binary files, and the Java input/output class hierarchy. Test your understanding of file reading/writing processes and their efficiency.