How to use Serialization?
The codes: |
Location:
|
1) import java.io.*; 2) Implement java.io.Serialization interface |
java class
|
1) Use writeObject and readObject methods whose header are as given below: a) void writeObject (Object obj) throws IOException; b) Object readObject() throws ClassNotFoundException, IOException; |
java application
|
The example below, shows a java application. This java application applies object from Class Student. The program code to do the following tasks:
- store the data into Student.txt using serializable (store object data into file)
- read data (objet from Student.txt)
- get the specific data, then store into text file (CS225.txt)
//Create the binary object file and text files
import java.io.*;
import java.util.Scanner;
public class StudentAPP {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
try
{
File outfile = new File("Student.txt"); //create the output binary file
FileOutputStream outStream = new FileOutputStream(outfile);
ObjectOutputStream objOutStream = new ObjectOutputStream(outStream);
File infile = new File("Student.txt"); //the input binary file to be read from
FileInputStream inputstream = new FileInputStream(infile);
ObjectInputStream objInStream = new ObjectInputStream(inputstream);
//to create the output text files
FileWriter outfilewriter1 = new FileWriter("CS225.txt");
BufferedWriter bufwriter1 = new BufferedWriter(outfilewriter1);
PrintWriter printwriter = new PrintWriter(bufwriter1);
//1) get input for 500 students and store into file (using object serializable)
for (int i=0; i < 500; i++)
{
System.out.print("\n noID : ");
String noid = scanner.next();
System.out.print("\n Name : ");
String name= scanner.next();
System.out.print("\n Program Code : ");
String programcode = scanner.next();
System.out.print("\n Program : ");
double program= scanner.nextDouble();
System.out.print("\n Number of Subjects : ");
int noSubject = scanner.nextInt();
System.out.print("\n Cost : ");
double cost = scanner.nextDouble();
Student stud = new Student(noid, name, programcode, program, noSubject, cost); //create the Student object
objOutStream.writeObject(stud); //write the Student object created into binary bile named Student.txt
}
//2 ) to read from the binary file named Student.txt and output to text files named CS225.txt
Student temp;
for (int i=0; i < 500; i++)
{
temp = (Student) objInstream.readObject(); //casting to access the members of the class Student
//3) to write to the text files named CS225 depending on the program code
if(temp.getProgramCode().equalsIgnoreCase("CS225"))
{
printwriter.println("No Id: + "temp.getNoid());
printwriter.println("Name: + "temp.getName());
printwriter.println("Program Code: + "temp.getProgramCode());
printwriter.println("Program: + "temp.getProgram());
printwriter.println("Number of Subject: + "temp.getNumberofSubject());
printwriter.println("Cost: + "temp.getCost());
}
}
objOutStream.close();
objInStream.close();
printwriter.close();
}
catch(ClassNotFoundExeption cnfe)
{
System.out.println(cnfe.getMessage());
}
catch(FileNotFoundExeption fnfe)
{
System.out.println(fnfe.getMessage());
}
catch(IOExeption ioe)
{
System.out.println(ioe.getMessage());
}
}
}
1 comment:
Is it legal to play slots online for real money? | DrmCD
It's 강릉 출장마사지 a legal and fair gaming state, and 구미 출장안마 the casino is open to all 세종특별자치 출장마사지 residents. The state also has state-mandated gaming information for 양산 출장마사지 every 세종특별자치 출장안마 single
Post a Comment