Lab 6 - Java Filing and Serialization
Lab 6 - Java Filing and Serialization
Lab-06
Java Filing and Serialization
Lab 6: Java Filing and Serialization
Table of Contents
1. Introduction 55
4. Concept Map 56
4.1. Implementing a simple file reader 56
4.1 Serialization 56
4.2 Deserialization 57
4.2.1 Streams to be used 57
4.2.2 Serializable objects: 57
6. Procedure& Tools 58
6.1 Tools 58
6.2 Setting-up JDK 1.7 [Expected time = 5mins] 58
6.2.1 Compile and run a Program 58
6.3 Walkthrough Task [Expected time = 30mins] 58
6.3.1 Implementing a simple file reader 58
6.3.2 Serialization 59
7. Practice Tasks 61
7.1 Practice Task 1 [Expected time = 20mins] 61
7.2 Practice Task 2 [Expected time = 20mins] 61
7.3 Practice Task 3 [Expected time = 20mins] 62
7.4 Out comes 62
7.5 Testing 62
9. Evaluation criteria 62
1. Introduction
You have learnt Java constructs, abstract classes and interface, file handling. In this lab, you will
test the theory of storing and retrieving data from files in the form of objects. Youwill
experiment this using serialization.
First important topic that you will study in this lab is the file handling mechanism of Java. Unlike
C++, Java has a very rich library for file handling. Basically Java divides the filing into two
different streams. One is a character stream and the other is byte stream. These both streams have
almost the same functionalities and the major difference is that the character stream reads and
write 16-bit character and on the other hand the byte stream as it is clear from its name can only
read write one byte at a time.
Sometimes, the data contained inside the object is very complex as an object can be composed of
many other complex objects. Java provides a simple alternate to this tedious task using
serialization. Object is represented in bytes format that can be stored and passed to the stream. In
case we need to retrieve the object we can reconstruct it from the saved/stored data stream.
¿ Writing the object is termed serialization or marshaling. Reading back the data into object form
is referred to as deserialization or un-marshaling of data. Objects to be serialized must implement
Serializable interface and the class corresponding to that object must be a public class. Any
attribute of the Serializable class which is non-static and non-transient is preserved. Static data
members are not preserved as they are the property of a class and not the property of the object
and Serialization is all about preserving the state of the object not the class. Transient fields are a
way provided by the serialization mechanism to skip the part of the object that is not relevant or
unimportant.
4. Concept Map
1. Create a file named input.txt in the current working directory. Save and close the file after
adding the following line in that text file.
import java.io.*;
3. Save the file by the name of FileReaderDemo. Compile and execute the program. You will
see the following line on the screen after the successful execution of the program.
4.1 Serialization
4.2 Deserialization
Deserialization refers to the mechanism of reading that sequence of bytes back into the object,
after which we can use it for further processing. Whole procedure is JVM independent, which
means an object can be serialized on one platform and de-serialized on an entirely different
platform.
Return type is void means object A will be written and nothing will be returned back. It may
throw IOException.
ObjectInputStream: This input stream reads and reconstructsbytes into Objects. It contains
method readObject as:
Return type is Object which means object needs to be parsed to appropriate data type after
reading.
The class whose objects need to be serialized must fulfill certain conditions as:
{
System.out.println("Name"+ name+ " Reg number" + regNumber
+"semester Number"+semesterNo );
}
}
You must solve the following problems at home before the lab.
After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you
6. Procedure& Tools
6.1 Tools
This task is designed to guide you towards serializing and externalizing objects.
Create a file named input.txt in the current working directory. Save and close the file after adding
the following line in that text file.
import java.io.*;
Save the file by the name of FileReaderDemo. Compile and execute the program. You will see
the following line on the screen after the successful execution of the program.
6.3.2 Serialization
2. Create a driver class like the following to finally write the preserve the data of the above
class.
import java.io.*;
public class SerializableExample
{
public static void main (String [] args )
{
3. Last step is to read the data back from the medium on which it was preserved. Following
class will show you how to deserialize the file.
import java.io.*;
public class SerializableExample
{
public static void main (String [] args )
{
Student Student_1 =new Student ();
try
{
FileInputStream fis=new FileInputStream ("first_file.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Student_1 =(Student) ois.readObject();
ois.close(); //closing steams.
fis.close();
}
catch (Exception Ex1)
{
Ex1.printStackTrace();
}
System.out.println("Deserialized student info ");
System.out.println("Name"+ Student_1 . name+ " Reg number" +
Student_1 .regNumber;
}
}
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\dataserver\assignments$\Advanced Computer Programming\Lab5
Using ArrayList of type” FlightRecords” add 5 flight records. Serialize and deserialize objects
sequentially.
Encryption is a technique to store data/information securely. You are required to make a class
Encryption. The class will contain following members: plaintext, key(int), encryptedText. Each
character of plain text will be encrypted by adding the key to ascii of the character. The key will
be shared by all the objects of the class and plaintext should not be saved to the file. Use
Serialization to save the object in the file.
Hint: Convert all the letters to uppercase/lowercase of the plaintext to encrypt it correctly.
Sample Input/ Output:
After completing this lab, student will be able to preserve the objects in-memory state using both
serialization.
7.5 Testing
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
10.1 Books
Text Book:
Java: How to Program by Paul J. Deitel, Harvey M. Deitel. Eighth Edition
Java Beginners Guide: http://www.oracle.com/events/global/en/java-outreach/resources/java-a-
beginners-guide-1720064.pdf
http://exampledepot.8waytrips.com/ for the package by package examples
10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available
at \\dataserver\jinnah$\