Java learning notes 41 (Properties class), learning notes properties

Source: Internet
Author: User
Tags map class

Java learning notes 41 (Properties class), learning notes properties

Properties enables persistent storage of collection data

It is an implementation class of the map interface. You can use the map class method,

But there is a difference: it does not have a generic type, specifying the key type as a string

 

This set will be frequently used in future development, such as connecting to a database.

This section briefly introduces the basic usage. The detailed usage is described later.

 

Storage obtains key-value pairs:

package demo;import java.util.Properties;import java.util.Set;public class PropertiesDemo {    public static void main(String[] args) {        function();    }        public static void function(){        Properties pro1 = new Properties();        pro1.setProperty("a", "1");        pro1.setProperty("b", "2");        pro1.setProperty("c", "3");        System.out.println(pro1);                String value = pro1.getProperty("c");        System.out.println(value);                Set<String> set = pro1.stringPropertyNames();        for(String key:set){            System.out.println(key+"="+pro1.getProperty(key));        }            }}

 

 

Collection Methods:

Load Method:

Load the key-value pairs in the file to the Collection

Create a new text file with the suffix changed to. properties and write the key-Value Pair

package demo;import java.io.FileReader;import java.io.IOException;import java.util.Properties;public class PropertiesDemo {    public static void main(String[] args) throws IOException {        function();    }        public static void function() throws IOException{        Properties pro1 = new Properties();        FileReader fr1 = new FileReader("d:\\pro.properties");                pro1.load(fr1);        fr1.close();        System.out.println(pro1);    }}

 

Store Method:

Write key-value pairs

Package demo; import java. io. fileWriter; import java. io. IOException; import java. util. properties; public class PropertiesDemo {public static void main (String [] args) throws IOException {function ();} public static void function () throws IOException {Properties pro1 = new Properties (); pro1.setProperty ("name", "zhangsan"); pro1.setProperty ("age", "18"); pro1.setProperty ("email ", "123456@qq.com"); FileWriter fWriter = new FileWriter ("d: \. properties "); pro1.store (fWriter," the reason "); // the second parameter is often omitted by fWriter. close ();}}

 

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.