Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Complete the following tasks, assuming that each applies to the same program: a) Write a statement that opens file "oldmast.ser" for input-use objectInputstream variable in01dMaster to wrap a FileInputStream object. b) Write a statement that opens file "trans.ser" for input-use ObjectInputStream variable inTransaction to wrap a FileInputStream object. c) Write a statement that opens file "newmast.ser" for output (and creation)-use \(0 \mathrm{b}-\) ject0utputStream variable outNewMaster to wrap a File0utputStream. d) Write a statement that reads a record from the file "oldmast.ser". The record is an object of class AccountRecordSerializable- use ObjectInputStream variable in01dMaster. Assume class AccountRecordSerializable is the same as the AccountRecordSerializable class in Fig. 14.17 e) Write a statement that reads a record from the file "trans.ser". The record is an object of class TransactionRecord - use ObjectInputStream variable inTransaction. f) Write a statement that outputs a record to the file "newmast.ser". The record is an object of type AccountRecordSerializable-use Object0utputStream variable outNewMaster.

Short Answer

Expert verified
Open files using FileInputStream and ObjectInputStream for reading, and FileOutputStream with ObjectOutputStream for writing.

Step by step solution

01

Opening 'oldmast.ser' for Input

To open 'oldmast.ser' for input and wrap it within an ObjectInputStream, we first create a FileInputStream. Then, we wrap it with an ObjectInputStream called 'in01dMaster'. ```java FileInputStream fisOldMaster = new FileInputStream("oldmast.ser"); ObjectInputStream in01dMaster = new ObjectInputStream(fisOldMaster); ```
02

Opening 'trans.ser' for Input

Similarly, for opening the file 'trans.ser', we create a FileInputStream and wrap it using an ObjectInputStream named 'inTransaction'. ```java FileInputStream fisTransaction = new FileInputStream("trans.ser"); ObjectInputStream inTransaction = new ObjectInputStream(fisTransaction); ```
03

Opening 'newmast.ser' for Output

To open 'newmast.ser' for output, we create a FileOutputStream and then wrap it with an ObjectOutputStream named 'outNewMaster'. This allows us to create and write objects to the file. ```java FileOutputStream fosNewMaster = new FileOutputStream("newmast.ser"); ObjectOutputStream outNewMaster = new ObjectOutputStream(fosNewMaster); ```
04

Reading a Record from 'oldmast.ser'

To read a record from 'oldmast.ser', which contains AccountRecordSerializable objects, we use the readObject method of the ObjectInputStream 'in01dMaster'. ```java AccountRecordSerializable accountRecord = (AccountRecordSerializable) in01dMaster.readObject(); ```
05

Reading a Record from 'trans.ser'

To read a TransactionRecord object from 'trans.ser', we similarly use the readObject method of 'inTransaction'. ```java TransactionRecord transactionRecord = (TransactionRecord) inTransaction.readObject(); ```
06

Writing a Record to 'newmast.ser'

To write an AccountRecordSerializable object to 'newmast.ser', we use the writeObject method of the ObjectOutputStream 'outNewMaster'. ```java outNewMaster.writeObject(accountRecord); ```

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

ObjectInputStream
The `ObjectInputStream` class in Java is a fundamental tool for reading serialized objects from files. When dealing with file operations in Java, especially when you want to read objects, `ObjectInputStream` becomes indispensable. It transforms data in a stream form back into a meaningful object structure. To use `ObjectInputStream`, you'll typically use it in combination with a `FileInputStream`. This means that the data, which is often stored in a file, is read as a sequence of bytes. The `FileInputStream` reads this sequence, and the `ObjectInputStream` then interprets these bytes as an object.

It’s essential to remember that `ObjectInputStream` works with files created through the `ObjectOutputStream`. This ensures that the format and the structure of the data are compatible. Here’s a simple breakdown:
  • Open a file using `FileInputStream`.

  • Wrap this stream using `ObjectInputStream`.

  • Utilize the `readObject()` method to retrieve objects from the file.
This process allows seamless reading of complex objects stored within files, adhering to Java’s object serialization mechanism.
FileInputStream
`FileInputStream` is a base tool for reading data streams from file sources in Java. It directly deals with the raw input of bytes from a file and is pivotal for various I/O operations. Imagine you have a file full of data that you need to process; this is where `FileInputStream` comes in. It reads data byte by byte, allowing programs to process it further. You often wrap it in other input classes, like `ObjectInputStream`, to handle more complex data types like objects. To use `FileInputStream`, you open a connection to the file you wish to read.
  • Initialize using: `FileInputStream fileInput = new FileInputStream("filename.ser")`.
  • Ensure the file exists, as it throws an exception if the file is missing.
  • Close the stream after reading to free system resources.

Given its importance, understanding `FileInputStream` is crucial for anyone involved in file manipulations, making it a cornerstone in Java’s file I/O operations.
ObjectOutputStream
The `ObjectOutputStream` class is used in Java to facilitate the serialization of objects into a file. This class makes it possible to write objects to a file in a form that can be later deserialized back into objects. Serialization in Java is a process where an object's state is transformed into a byte stream. `ObjectOutputStream` aids in carrying out this task by taking an object and writing its state, serialized, into a file. Here’s how you can work with `ObjectOutputStream`:
  • Open a file using a `FileOutputStream`.

  • Wrap this stream using `ObjectOutputStream`.

  • Use `writeObject(object)` to serialize the object and send it to the file.
One of the essential practices while using `ObjectOutputStream` is ensuring that the class structure of the object is maintained. Alterations might lead to invalid data during deserialization.
Serialization in Java
Serialization in Java is a technique used to convert an object into a byte stream, thus making it easy to store an object or transmit it over a network. This technique is crucial for Java developers when data needs to be persisted or shared. Serialization ensures that all the state of an object, except for the transient and static fields, is captured and stored. This is achieved by implementing the `Serializable` interface, which marks a class as serializable. Key points about serialization:
  • The `Serializable` interface is marker-based and does not contain any methods.
  • Use `ObjectOutputStream` to serialize an object to a file.

  • Deserialization, the reverse process, reads back these bytes to recreate the object, done using `ObjectInputStream`.

  • Always maintain the version of a class using `serialVersionUID` to handle deserialization compatibility.
Serialization is extremely beneficial when running object communication in distributed systems or storing application states efficiently.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Determine which of the following statements are true and which are false. If \(f a l\) se, explain why. a) The programmer must explicitly create the stream objects System. in, System. out and System .err. b) When reading data from a file using class Scanner, if the programmer wishes to read data in the file multiple times, the file must be closed and reopened to read from the beginning of the file. This moves the file-position pointer back to the beginning of the file. c) Method exists of class File returns true if the name specified as the argument to the File constructor is a file or directory in the specified path. d) Binary files are human readable. e) An absolute path contains all the directories, starting with the root directory, that lead to a specific file or directory. f) Class Formatter contains method printf, which enables formatted data to be output to the screen or to a file.

Determine which of the following statements are true and which are false. If false, explain why. a) The impressive functions performed by computers essentially involve the manipulation of zeros and ones. b) People specify programs and data items as characters. Computers then manipulate and process these characters as groups of zeros and ones. c) Data items represented in computers form a data hierarchy in which data items become larger and more complex as we progress from fields to characters to bits and so on. d) A record key identifies a record as belonging to a particular field. companies store all their information in a single file to facilitate computer processing of the information. When a program creates a file, the file is retained by the computer for future reference.

Find the error in each block of code and show how to correct it. a) Assume that account, company and amount are declared. Object0utputStream outputStream; outputStream.writeInt( account ); outputStream.writeChars( company ); outputStream.writeDouble ( amount ); b) The following statements should read a record from the file "payables.txt". The Scanner variable inPayable should be used to refer to this file. Scanner inPayable = new Scanner( new File( "payables.txt" ) ); PayablesRecord record = ( PayablesRecord ) inPayable.readobject();

Fill in the blanks in each of the following statements: a) Ultimately, all data items processed by a computer are reduced to combinations of ________ and _________ b) The smallest data item a computer can process is called a(n) ____________ c) \(A(n)\) ___________ can sometimes be viewed as a group of related records. d) Digits, letters and special symbols are referred to as _________ e) A database is a group of related ___________ f) Object _____________ normally enables a program to output error messages to the screen.

Fill in the blanks in each of the following statements: a) Computers store large amounts of data on secondary storage devices as __________ b) \(A(n)\) ____________ is composed of several fields. c) To facilitate the retrieval of specific records from a file, one field in each record is chosen as a \((n)\) ____________ d) Files that are created using byte-based streams are referred to as _________ files, while files created using character-based streams are referred to as ___________ files. e) The standard stream objects are __________, _________ and ____________

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free