"Java programming" IO Stream operation object __ Programming

Source: Internet
Author: User
the method of the Flow action object:

ObjectInputStream and ObjectOutputStream two objects, objects saved with ObjectOutputStream can only be read with ObjectInputStream serializable serialization:

When each class implements serialiable serialization, Java generates a UID based on the class's member variables and member functions to differentiate each of the different classes, and of course, you can set this UID yourself manually.

The method is set to: Static final long serialversionuid = 42L; serializable function:

When a class implements the serializable, the ObjectOutputStream is used to store objects created by this class in a file, which generates a UID corresponding to this class, and the next time, with ObjectInputStream

When you read an object in this file, it matches the same UID class. If the original class changes, it will generate a new UID, at this point, read the object in the file, it will error classnotfoundexception; Of course, this

, we can also fix the UID of the class, which can be read even if the corresponding class of the object in the file is changed. Serializable scope:

Only objects stored in the heap memory can be serialized. If you are a static member stored in a static area, it cannot be serialized. Of course, if you have a member that you don't want to be serialized, you can add the Transient keyword before the member.

Here's an example of a stream manipulation object:

Import java.io.*;
Class Objectstreamdemo 
{public
	static void Main (string[] args) throws IOException
	{
		readobj ();
	}
	//Storage object to file public
	static void Writeobj () throws IOException
	{
		//Create ObjectOutputStream object
		ObjectOutputStream oos=new ObjectOutputStream (New FileOutputStream ("Object.txt"));
		Oos.writeobject (New person ("Enen", 2));
		Oos.close ();
	}
	Read and print the objects in the file to public
	static void Readobj () throws IOException
	{
		//Create ObjectInputStream read object stream
		ObjectInputStream ois=new ObjectInputStream (New FileInputStream ("Object.txt"));
		Person P=null;
		Try
		{
			p= (person) ois.readobject ();			
		}
		catch (ClassNotFoundException e)
		{
			throw new RuntimeException (e.tostring ());
		}
		SYSTEM.OUT.PRINTLN (P);		
	}

Import java.io.*;
Class Person implements Serializable
{
	String name;
	int age;
	Person (String Name,int age)
	{
		this.name=name;
		this.age=age;
	}
	Public String toString ()
	{return
		name+ "::" +age;
	}
}





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.