Java IO stream-properties

Source: Internet
Author: User

2017-11-05 21:37:50

    • Properties

Properties: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.

Properties is an attribute collection class that is a collection class that can be combined with an IO stream.

The properties class can be stored in a stream or loaded from a stream, which is a subclass of Hashtable, or a subclass of map.

* Construction Method

* Common Methods

~ Properties as the use of the map collection

public class Main {public    static void Main (string[] args) {        //No generics, not generic-class        Properties prop = new properties (); c3/>//add element        prop.put ("1", "Hello");        Prop.put ("2", "World");        Prop.put ("3", "!");        Traversal set        set<object> set = Prop.keyset ();        For (object K:set) {            object val = Prop.get (k);            System.out.println (k + "---" +val);}}}    

~ Special Features of properties

    1. Public Object setProperty (String key,string value)
    2. public string GetProperty (string key)
    3. Public set<string> Stringpropertynames ()
public class Main {public    static void Main (string[] args) {        //No generics, not generic-class        Properties prop = new properties (); c3/>//add element        prop.setproperty ("1", "Hello");        Prop.setproperty ("2", "World");        Prop.setproperty ("3", "!");        Traversal set        set<string> set = Prop.stringpropertynames ();        for (string k:set) {            string val = Prop.getproperty (k);            System.out.println (k + "---" +val);}}}    

~ Combination of properties and IO streams

    1. public void load (Reader reader): The data in the file is read into the Properties collection, and the data in the file must be in the form of a key-value pair.
    2. public void Store (Writer writer,string Comments): Stores the data in the collection into a file.
        No generics, not generic-class        Properties prop = new properties ();        add element        Prop.setproperty ("1", "Hello");        Prop.setproperty ("2", "World");        Prop.setproperty ("3", "!");        Writer w = new FileWriter ("E:/test.txt");        Prop.store (W, "HelloWorld");
#helloworld #sun Nov 22:06:16 CST 20171=hello2=world3=\!
        Properties prop = new properties ();        Reader r = new FileReader ("E:/test.txt");        Prop.load (r);        R.close ();

Java IO stream-properties

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.