Javaio Stream of objects

Source: Internet
Author: User
1. Why to use object flow.

In the past when we write data to a file on disk, we package the data and write it to the text of the disk in bytes or streams of characters. If the data we want to write is an object with a username and password written to the disk text, then we need to concatenate the username and password into a string and then write to the text on the disk. So the next time we need to read the text on the disk, we need to read out the string, parsing, and then used for operation, very troublesome. And the object flow directly to the object packaging and then to file flow output to disk, there is no need to do string concatenation and other operations. Convenient 2. Object output Stream Implementation

public class Objectoutputstreamclass {         public static void WriteFile () throws Ioexce ption{        user user=new User ("admin", "123456");       
 file file=new File ("E://a.txt");
        fileoutputstream fileoutputstream=new fileoutputstream (file);         objectoutputstream objectoutputstream=new ObjectOutputStream (
FileOutputStream);
        objectoutputstream.writeobject (user);
        objectoutputstream.close ();    &nbsp}     public static void Main (string[] args) throws IOException, classnotfoundexception {        writefile ();    &nbsp}} class User implements  serializable{    /**      *     /    private static final Long Serialversionuid = 1L;
    private String username;
    private String password;     public User (string Username, string password) {        super ();  
       this.username = username;
        this.password = password;    &nbsp      @Override     public String toString () {    
    return "User [username=" + Username + ", password=" + password + "]";
    &nbsp}}

Results:



3. Implementation of object input stream

public class Objectinputstreamclass {public static void ReadFile () throws IOException, classnotfoundexception{File
		File=new File ("E://a.txt");
		FileInputStream fileinputstream=new fileinputstream (file);
		ObjectInputStream objectinputstream=new ObjectInputStream (FileInputStream);
		User user= (user) objectinputstream.readobject ();
	SYSTEM.OUT.PRINTLN (user);
	public static void Main (string[] args) throws ClassNotFoundException, IOException {readFile (); 
} <pre name= "code" class= "HTML" >class User implements serializable{    /**      *
    /    private static final long serialversionuid = 1L;
    private String username;
    private String password;     public User (string Username, string password) {        super ();  
       this.username = username;         this.password = password;    &nbsp      @Override     public String toString () {    
    return "User [username=" + Username + ", password=" + password + "]";    &nbsp}}

Results:

User [Username=admin, password=123456]

----------------------------------------------- 4. Summary

It is important to note that the object must be serialized, or else the exception will be reported as not serialized.

<1>. If the object needs to be written to a file, then the class to which the object belongs must implement the Serializable interface. The serializable interface does not have any method, just an identity interface.
<2> The deserialization of an object does not invoke the constructor method when it is created.
<3>.serialversionuid is used to record the version information of the class file, Serialversionuid this number is a number of the class name, the member, the package name, and the project name.
<4>: When using ObjectInputStream deserialization, ObjectInputStream reads the serialversionuid in the file first. Then the serialversionuid of the local class file is compared, and if the two IDs are inconsistent, deserialization fails.
<5> If the members of a class may be modified while serializing and deserializing, it is best to specify a Serialversionuid for the class at the outset, if a class has already specified a serialversionuid, and then serialize and deserialize the The JVM will no longer count the serialversionuid of this class itself.






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.