Java serialization example

Source: Internet
Author: User

Java serialization example

The Seerializable interface does not have any method, so the implementation is still empty ., It is called the mark interface package serialization; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. objectInputStream; import java. io. objectOutputStream; import java. io. serializable; public class ObjectSerializationApp {public static void main (String [] args) {// TODO Auto-generated method stubObjectOutputStream objectWriter = null; ObjectInputStream objectReader = null; try {objectWriter = new ObjectOutputStream (new FileOutputStream ("student. dat "); objectWriter. writeObject (new Student (1, "John", "Mayor"); objectWriter. writeObject (new Student (2, "Sam", "Abel"); objectWriter. writeObject (new Student (3, "Anita", "Motwani"); System. out. println ("print the list that exists in the student database"); objectReader = new ObjectInputStream (new FileInputStream ("student. dat "); for (int I = 0; I <3; I ++) {// method readObject () returns a Student object, this Student object is also printed to the Console System by calling the implicit override toString () method. out. println (objectReader. readObject () ;}} catch (Exception e) {// TODO: handle finished tione. printStackTrace ();} finally {try {objectWriter. close (); objectReader. close ();} catch (Exception e2) {// TODO: handle implements tione2.printstacktrace () ;}}// Student class Student implements Serializable {private String firstName; private String lastName; private int id; public Student (int id, String firstName, String lastName) {// TODO Auto-generated constructor stubthis. id = id; this. firstName = firstName; this. lastName = lastName;} // rewrite the toString () method public String toString () {return ("ID:" + id + "" + firstName + "" + lastName );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.