Object-Oriented Programming SWE211
23 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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?

Binary files

Text files can be read by the JVM.

False (B)

Which of these classes are a subclass of OutputStream in Java? (Select all that apply)

<p>FilterOutputStream (A), ObjectOutputStream (E), BufferedOutputStream (F)</p> Signup and view all the answers

What is the return type of the read() method in InputStream?

<p>An <code>int</code> that represents a byte.</p> Signup and view all the answers

What is the purpose of FilterInputStream and FilterOutputStream?

<p>To filter bytes for specific purposes.</p> Signup and view all the answers

It is possible to create a FileInputStream with a non-existent file.

<p>False (B)</p> 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?

<p>The existing file will be deleted.</p> Signup and view all the answers

What constructor(s) of FileOutputStream can be used to append data to a file?

<p>The constructors that accept a boolean <code>append</code> parameter and set it to <code>true</code>.</p> Signup and view all the answers

What is the advantage of using DataInputStream and DataOutputStream?

<p>They allow reading and writing primitive data types and strings more conveniently.</p> Signup and view all the answers

What are the primary methods provided by DataInputStream and DataOutputStream for writing primitive data types?

<p>Their primary methods are ones like <code>writeInt()</code>, <code>readDouble()</code>, <code>writeChar()</code>, and <code>writeUTF()</code>. These methods are often used to write and read data from files and streams.</p> Signup and view all the answers

The Serializable interface has methods that must be implemented.

<p>False (B)</p> Signup and view all the answers

What is the role of ObjectInputStream and ObjectOutputStream?

<p>They are used to read and write objects to streams.</p> Signup and view all the answers

All objects can be written to a stream.

<p>False (B)</p> Signup and view all the answers

What is the benefit of implementing the Serializable interface? (Select all that apply)

<p>It allows the JVM to automatically manage the serialization process. (A), It eliminates the need for the user to write custom code for serialization and deserialization. (C), It simplifies the process of storing objects in a file or network (D)</p> Signup and view all the answers

What does the readObject method of ObjectInputStream do?

<p>It reads the data from a stream and recreates an object.</p> Signup and view all the answers

An array is serializable if at least one of its elements are serializable.

<p>False (B)</p> Signup and view all the answers

What is the key difference between sequential files and random access files?

<p>Sequential files can only be read or written in a linear order, while random access files allow reading or writing at any specific location within the file.</p> Signup and view all the answers

The RandomAccessFile class allows for both reading and writing to files.

<p>True (A)</p> Signup and view all the answers

The seek() method in RandomAccessFile moves the file pointer to the beginning of the file.

<p>False (B)</p> Signup and view all the answers

Which of the following methods can be used in RandomAccessFile? (Select all that apply)

<p>writeChars() (A), readInt() (B), writeChar() (C), length() (D), seek() (E), readLine() (F), writeDouble() (G)</p> 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.

<p><code>RandomAccessFile raf = new RandomAccessFile(&quot;test.dat&quot;, &quot;rw&quot;);</code></p> Signup and view all the answers

Write a method call in RandomAccessFile to create a new file that only allows reading.

<p><code>RandomAccessFile raf = new RandomAccessFile(&quot;test.dat&quot;, &quot;r&quot;);</code></p> Signup and view all the answers

Flashcards

File Object

Represents a file or path, but doesn't handle reading/writing data.

Input/Output (I/O)

Reading and writing data to/from files or devices.

Scanner class

Used for reading data from text files.

PrintWriter class

Used for writing data to text files.

Signup and view all the flashcards

Text File

Stores data in human-readable format.

Signup and view all the flashcards

Binary File

Stores data in binary form, designed for program use.

Signup and view all the flashcards

FileInputStream

Used to read from a binary file.

Signup and view all the flashcards

FileOutputStream

Used to write to a binary file.

Signup and view all the flashcards

FileInputStream Constructor (String)

Creates a FileInputStream using a filename.

Signup and view all the flashcards

FileInputStream Constructor (File)

Creates a FileInputStream using a File object.

Signup and view all the flashcards

FileNotFoundException

Error occurs if the file does not exist in FileInputStream.

Signup and view all the flashcards

FileOutputStream Constructor (String)

Creates a FileOutputstream using a filename.

Signup and view all the flashcards

FileOutputStream Constructor (File)

Creates a FileOutputStream using a File object.

Signup and view all the flashcards

FileOutputStream Constructor (boolean append)

Creates a FileOutputStream for appending.

Signup and view all the flashcards

InputStream

Reads data from a source.

Signup and view all the flashcards

OutputStream

Writes data to a destination.

Signup and view all the flashcards

Binary Data

Data represented in bits (0s and 1s).

Signup and view all the flashcards

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 and FileOutputStream.

FilterInputStream/FilterOutputStream

  • Filter streams modify basic byte input/output streams for various purposes.
  • DataInput and DataOutput 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, extending FilterInputStream.
  • Reads primitive data types from a stream.

DataOutputStream

  • Implements DataOutput interface, extending FilterOutputStream.
  • 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 and DataOutputStream on in.dat and out.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 or ObjectOutputStream using constructors ObjectInputStream(InputStream in) and ObjectOutputStream(OutputStream out)

Example of object handling (example program)

  • Example saving object data to foo.bin file, demonstrates ObjectOutputStream

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 objectFoo instances to the file foo.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 and DataOutputStream) 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.

Quiz Team

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.

More Like This

Use Quizgecko on...
Browser
Browser