JP 5 2 Practice Solution
JP 5 2 Practice Solution
JP 5 2 Practice Solution
Lesson Objectives:
• Use streams to read and write files
• Read and write objects by using serialization
Vocabulary:
Identify the vocabulary word for each definition below.
Output Stream Sequences of bytes or characters transmitted from one program to another program or a
file.
Back quoting A back slash in front of another character makes Java treat the next character as an
ordinary character.
Input Stream Sequences of bytes or characters transmitted from another program or user's activity.
Standard Error This is the output stream for error raised by a program, also known as the second channel.
Resolve Path The join of two related paths, or the remaining part of a join after subtracting part of the
path.
Streams Types of standard input, output, and error; and qualified object types in Java.
Standard Output The output for debugging message and ordinary reports and messages.
1. Create a class with a static main that tests the ability to resolve and print a Path:
a) Create an instance of a FileSystem class.
b) Resolve an instance of a Path interface from a directory and filename path.
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
c) Print the constructed Path with System.out.println() method.
2. Open the accountgenerator project that was introduced in the practice exercises for section 4_1.
a) The Employee class should implement the Serializable interface so that it can be Serialized.
public class Employee implements Serializable{
b) To test serialization create a serializeData method in the AccountGenerator class that accepts an Employee object as a
parameter and does not return a value. Serialize the object to file named employee.ser in the project folder.
static void serializeData(Employee emp) {
try(FileOutputStream fos = new FileOutputStream("employee.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);) {
oos.writeObject(emp);
System.out.print("Serialized data is saved in employee.ser");
} catch (IOException i) {
i.printStackTrace();
}//end try catch
}//end method serializeData
c) To test de-serialization create a deSerialize method in the AccountGenerator class that deoes not accept any parameters
and returns an Object value. The deSerialize method should throw a classNotFoundException. The object should be
read from the employee.ser file in the project folder.
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
2
d) To test de-serialization create a deSerialize method in the AccountGenerator class that deoes not accept any parameters
and returns an Object value. The deSerialize method should throw a classNotFoundException. The object should be
read from the employee.ser file in the project folder.
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.