ObjectInputStream and ObjectOutputStream implement object access. objectoutputstream

Source: Internet
Author: User
Tags object serialization

ObjectInputStream and ObjectOutputStream implement object access. objectoutputstream

1.

Objects read and written by the ObjectInputStream and ObjectOutputStream classes must implement the Serializable interface. The transient and static member variables in the object are not read or written.

2.

Serializable is an object serialization interface. Only serialization can achieve object access.

3.

// Define the Student Category

Package job;

Import java. io. Serializable;
Import java. util. Set;

Public class Student implements Serializable {// note the serialization object
Private static final long serialVersionUID =-7311905363747695209L;
Private String name;
Private int age;
Private int id;
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getAge (){
Return age;
}
Public void setAge (int age ){
This. age = age;
}
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public Student (String name, int age, int id ){
Super ();
This. name = name;
This. age = age;
This. id = id;
}
Public Student (){
Super ();
// The constructor stub automatically generated by TODO
}
}

// Use dynamic arrays to access objects

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------

Package job;

Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. ObjectInputStream;
Import java. io. ObjectOutputStream;
Import java. io. OutputStream;
Import java. util. ArrayList;

Public class ObjectStreamDemo {
Public static void main (String [] args) throws Exception, ClassNotFoundException {
// Create a file
OutputStream f = new FileOutputStream ("a.txt ");
// Prepare the new object for storage
Student stu = new Student ("John", 16,100 1 );
Student stu1 = new Student ("Li Si", 15,100 2 );
// Construct the list and save it to the object
ArrayList <Object> link = new ArrayList <Object> ();
Link. add (stu );
Link. add (stu1 );
// Convert the list to an object Array
Object [] stuarr = link. toArray ();
// Write a file using an object Array
ObjectOutputStream oos = new ObjectOutputStream (f );
Oos. writeObject (stuarr );
Oos. close ();
// Read the object
InputStream fi = new FileInputStream ("a.txt ");
ObjectInputStream ois = new ObjectInputStream (fi );
// Read the object to the object Array
Object [] obj = (Object []) ois. readObject ();
For (int I = 0; I <obj. length; I ++ ){
Student s = (Student) obj [I];
System. out. print ("name" + s. getName () + "\ t ");
System. out. print ("Age" + s. getAge () + "\ t ");
System. out. println ("Learn" + s. getId ());
}
Ois. close ();
}

}

4.

Another method (FileOutputStream (fn, true) append an object)

Package com;

Import java. io. IOException;
Import java. io. ObjectOutputStream;
Import java. io. OutputStream;

Public class My extends ObjectOutputStream {
Public My () throws IOException {
Super ();
}

Public My (OutputStream out) throws IOException {
Super (out );
}

@ Override
Protected void writeStreamHeader () throws IOException {
Return;
}

}

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------

 

Package com;

 

Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. ObjectInputStream;
Import java. io. ObjectOutputStream;
Import java. text. ParseException;
Import java. text. SimpleDateFormat;

 

 

Public class ObjectStreamDemo {

 

Public static void main (String [] args) throws FileNotFoundException, IOException, ParseException, ClassNotFoundException {
SimpleDateFormat sdf = new SimpleDateFormat ("MM dd, yyyy ");
String fn = "e:/db. dat ";
ObjectInputStream ois = new ObjectInputStream (new FileInputStream (fn ));

 

While (true ){
Try {
Student stu = (Student) ois. readObject ();
System. out. println (stu. getName ());
System. out. println (stu. getAge ());
System. out. println (stu. getBirthday (). toLocaleString ());
System. out. println ("-----------------------------------------");

 

} Catch (Exception e ){
Break;
}
}

 

// Object output stream, which stores java objects to files
Student s1 = new Student ("Li Si", 22, sdf. parse ("May 20, 1985 "));
ObjectOutputStream oos;
If (new File (fn). length () <1 ){
Oos = new ObjectOutputStream (new FileOutputStream (fn, true ));
} Else {
Oos = new My (new FileOutputStream (fn, true ));
}
Oos. writeObject (s1 );
Oos. close ();

 

}
}

 

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------

 


Question about ObjectInputStream and ObjectOutputStream in JAVA

This object must be serializable first, and then multiple objects should be stored in the container, such as ArrayList <?> List;
Then, the list is serialized for storage and read as a string of objects ~~

[Java problem] Reading data in an Object fails after using ObjectOutputStream and ObjectInputStream to store the entire Object. Why?

I found the problem. It turned out to be very simple.
The problem lies in your Save class. You reference a sequence class in the Save class, which is not serialized. That is to say, the sequence class does not implement Serializable, the requirement of implements Serializable is that all implements Serializable classes, the declared classes called below, also need to be serialized, that is, implements Serializable, so no.
Solution:
Put all input operations in the Test class, and then pass the input parameters to ask () to solve the problem.

Related Article

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.