You are on page 1of 3

Java Programming

5-2: Input and Output Fundamentals


Practice Solutions

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.

Deserialization The process of converting a stream of data into an object.

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.

Deserialization The process of converting a stream of data into an object.

Input Stream Sequences of bytes or characters transmitted from another program or user's activity.

Hard Link A file name that maps to another file.

Normalized Path A path that is direct rather than indirect.

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.

Standard Input Any keyboard, mouse, or touchscreen input to a program.

Serialization The process of converting an object or file to a series of bytes.

Try It/Solve It:

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.

public class PathTestCase1


{
public static void main(String[] args) {
FileSystem fs = FileSystems.getDefault();
Path p1 = Paths.get("C:/JavaProgramming/gameData/scores");
Path p2 = Paths.get("Highscores.txt");
Path p3 = p1.resolve(p2);
Path p = fs.getPath(p3.toString());
System.out.println("Default Directory [" + p + "]");
}//end method main
}//end class PathTestCase1

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.

public static Object deSerialize() throws ClassNotFoundException {


try(FileInputStream fis = new FileInputStream("employee.ser");
ObjectInputStream ois = new ObjectInputStream(fis)) {
return ois.readObject();
} catch (IOException i) {
i.printStackTrace();
return null;
}//end try catch
}//end method deSerialize

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.

public static void main(String[] args) throws ClassNotFoundException {


Employee emp = new Employee();
serializeData(emp);
Employee emp2 = (Employee) deSerialize();
System.out.println(emp2);
}//end method main

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.

You might also like