Java--properties, serialized stream, and deserialization stream

Source: Internet
Author: User
Tags object serialization serialization

first, the properties class 1, Properties class Introduction

The Properties class represents a persistent set of attributes. Properties can be saved in a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

Characteristics:

(1), the subclass of Hashtable, the methods in the Map collection can be used.

(2), the collection does not have a generic type. The key values are all strings.

(3), which is a set of properties that can be persisted. The key values can be stored in the collection or stored on a persistent device (hard disk, USB flash drive, CD-ROM). The source of the key value can also be a persistent device.

(4), with the method of combining with the flow technology.

2. Common Methods

Load (InputStream) reads the data from the file corresponding to the specified stream and saves it to the Propertie collection

Load (Reader)

Store (outputstream,commonts) saves the data in the collection to the file corresponding to the specified stream, and the parameter commonts represents the description information

Stroe (writer,comments);

Code implementation:

1     Properties Pro=new properties ();2 //Save Data3 pro.setproperty ("Driver", "Com.mysql.jdbc.driver");4 pro.setproperty ("Uesername", "root");5 pro.setproperty ("Password", "123456");6 //Fetch data7 //Get the collection of keys8 set<string> set=pro.stringpropertynames ();9 //Find values by keyTen For (String s:set) { One System.out.println (s+ "..." +pro.getproperty (s)); A     }

Read key-value pairs from the properties file-code implementation:

1  Public Static voidMethod1 ()throwsioexception{2         //reading key-value pairs from the properties file3Properties pro=NewProperties ();4         //Clear Data Source5FileInputStream fis=NewFileInputStream ("E:\\test\\demo1.properties");7 pro.load (FIS);8 fis.close ();9         //TraverseTenSet<string> set=pro.stringpropertynames (); One          for(String s:set) { ASystem.out.println (s+ "..." +Pro.getproperty (s)); -         } -}

Write key-value pairs in the properties file via the Properties collection-code implementation:

1  Public Static voidMETHOD2 ()throwsioexception{2         //writes a key value pair to a file through the properties3         //Clear Destination4FileOutputStream fos=NewFileOutputStream ("E:\\test\\demo2.properties");6Properties pro=NewProperties ();7Pro.setproperty ("name", "Baba");8Pro.setproperty ("Age", "85");9Pro.store (FOS, "hello");//"Hello" is a note that can be written without writingTen}
second, serialized stream and deserialization stream

The flow of operations used to read an object from a stream objectinputstream called a deserialization stream

the flow of operations used to write objects to the stream ObjectOutputStream is called a serialized stream

Features: For manipulating objects. You can write an object to a file, or you can read an object from a file.

1. object Serialization Stream ObjectOutputStream

Person class:

1  Packagecom.oracle.Properties;2 3 Importjava.io.Serializable;4 5  Public classPersonImplementsserializable{6     PrivateString name;7     Private transient intAge//Transient keyword-this property is not serialized when serialized8     //Define serial number (fixed dead)9     Private Static Final Longserialversionuid=123456789l; Ten      PublicPerson (String name,intAge ) { One         Super(); A          This. Name =name; -          This. Age =Age ; -     } the      -      PublicString GetName () { -         returnname; -     } +      Public voidsetName (String name) { -          This. Name =name; +     } A      Public intGetage () { at         returnAge ; -     } -      Public voidSetage (intAge ) { -          This. Age =Age ; -     } - @Override in      PublicString toString () { -         return"Person [name=" + name + ", age=" + Age + "]"; to     } +}

Test class:

1  Public Static voidMETHOD3 ()throwsioexception{2Person p=NewPerson ("Zhang San", 18);3         //Clear Destination4FileOutputStream fos=NewFileOutputStream ("E:\\test\\person.txt");5         //creating a serialized stream6ObjectOutputStream oos=NewObjectOutputStream (FOS);7         //writing to an object8 Oos.writeobject (p);9         //Freeing ResourcesTen Oos.close ();  One}

2. Object deserialization stream ObjectInputStream

Test class:

1     //deserialization2      Public Static voidMETHOD4 ()throwsIOException, classnotfoundexception{3         //Clear Data Source4FileInputStream fis=NewFileInputStream ("E:\\test\\person.txt");5         //creating a Deserialization object6ObjectInputStream ois=NewObjectInputStream (FIS);7         //deserialization8Person p=(person) ois.readobject ();9 System.out.println (p);Ten}

3. Serialization interface

Characteristics:

1. When an object is to be serialized, the class to which the object belongs must implement the Serializable interface. ( otherwise an exception notserializableexception exception will occur.) )

2. when deserializing an object, an exception invalidclassexceptionoccurs if the object's class file is modified after serialization . This exception occurs for the following reasons:

(1) The sequence version number of the class does not match the version number of the class descriptor read from the stream

(2) This class contains unknown data types

(3) There is no accessible parameterless construction method for this class

3.Serializable labeled interface. This interface provides a sequence version number for classes that require serialization. Serialversionuid. The purpose of this version number is to verify that the serialized object and the corresponding class are version-matched.

4. transient keyword transient

when an object of a class needs to be serialized, some attributes do not need to be serialized, and attributes that do not need to be serialized can be decorated with the keyword transient . This property is not serialized when serialized, as long as the transient is modified.

Static adornments are not serialized at the same time, because serialization is the persistent storage of object data, while static data belonging to the class load is not serialized.

Java--properties, serialized stream, and deserialization stream

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.