Java->properties class

Source: Internet
Author: User

Properties Class Introduction

Propertiesclass represents a persisted set of properties. Propertiescan 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,Hashtable of the sub-class,map collection of methods can be used.

2. The collection does not have a generic type. The key values are all strings .

3, it 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 flow of technology combined with the method.

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

L Load (Reader)

L 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

L Stroe (writer,comments);

Code Demo:

/*

*

* Properties collection, which is the only collection that can interact with IO streams

*

* Requirements: Add elements to the Properties collection and traverse

*

Method

* Public Object SetProperty (string key, String value) calls the Hashtable method Put.

* Public set<string> stringpropertynames () returns the set of keys in this property list.

* Public string GetProperty (string key) to search for properties in this property list with the specified key

*/

Public class PropertiesDemo01 {

Public Static void Main (string[] args) {

To create a collection object

Properties prop = New properties ();

Adding elements to a collection

Prop.put (key, value);

Prop.setproperty ("Zhou Xun", "Jacky Cheung");

Prop.setproperty ("Li Xiaolu", "Jia Nailiang");

Prop.setproperty ("Yang Mi", "Jacky Lau Wei");

SYSTEM.OUT.PRINTLN (prop);//Use of test

Iterating through the collection

set<string> keys = Prop.stringpropertynames ();

for (String Key:keys) {

Find Values by key

Prop.get (Key)

String value = Prop.getproperty (key);

System. out. println (key+ "= =" +value);

}

}

}

to store the contents of a collection in a file

Requirements: Use the Properties collection to complete the operation of storing the contents of the collection in the corresponding file of the IO stream

Analysis:

1, create Properties collection

2, adding elements to the collection

3, create a stream

4. Store the data in the collection in the file corresponding to the stream

Stroe (Writer, comments)

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 (uncoide encoding )

5, close the stream

Code Demo:

Public class PropertiesDemo02 {

Public Static void Main (string[] args) throws IOException {

1, create Properties collection

Properties prop = New properties ();

2, adding elements to the collection

Prop.setproperty ("Zhou Xun", "Jacky Cheung");

Prop.setproperty ("Li Xiaolu", "Jia Nailiang");

Prop.setproperty ("Yang Mi", "Jacky Lau Wei");

3, create a stream

FileWriter out = new FileWriter ("prop.properties");

4. Store the data in the collection in the file corresponding to the stream

Prop.store (out, "save data");

5, close the stream

Out.close ();

}

}

Reads the data from the file and saves it to the collection

Requirements: Extract data from the attribute set file Prop.properties and save to the collection

Analysis:

1, creating the collection

2, creating a Stream object

3. Read data from the corresponding file in the stream to the collection

Load (InputStream) The data in the file corresponding to the specified stream is read and saved to the Propertie Collection

Load (Reader)

4, close the stream

5, displaying the data in the collection

Code Demo:

Public class PropertiesDemo03 {

Public Static void Main (string[] args) throws IOException {

1, creating the collection

Properties prop = New properties ();

2, creating a Stream object

FileInputStream in = new fileinputstream ("prop.properties");

FileReader in = new FileReader ("Prop.properties");

3. Read data from the corresponding file in the stream to the collection

prop.load (in);

4, close the stream

In.close ();

5, displaying the data in the collection

System. out. println (prop);

}

}

Note: You can use the character stream FileReader to complete the Chinese read operation in the file.

Java->properties class

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.