Java read and write operations to properties configuration files

Source: Internet
Author: User

1.1. Read 1.1.1 to the properties profile. The file to read

configuration file Key and the value with a colon between [:] and equals sign [=] it's all possible. .

Test.properties

Name:\u5f20\u4e09

Password:\u5bc6\u7801

Age:

1.1.2. program code to read

Propertiestest.java

/**     * Read config file     * @throws ioexception     *    /@Test public    Void Read () throws ioexception{       Properties Properties = new properties ();             File File = new file ("Test.properties");             FileInputStream fis = new FileInputStream (file);             Properties.load (FIS);             set<object> KeySet = Properties.keyset ();             for (Object object:keyset) {           System.out.println (object.tostring () + ":" +properties.getproperty (object.tostring ()));       }    }


1.1.3. Results

Background Printing results

Age:22

Password: password

Name: Zhang San

1.2. Write to the Propertiest configuration file 1.2.1. Code that writes the program

Propertiestest.java

/**     * Write content to config file     * @throws ioexception     *    /@Test public    Void Write () throws IOException {       Properties Properties = new properties ();             String file = "Aa.properties";       FileOutputStream fos = new FileOutputStream (file);             Properties.setproperty ("AA", "123");       Properties.setproperty ("BB", "456");       Properties.setproperty ("CC", "789");       Properties.setproperty ("name", "Zhang San");                   Properties.store (FOS, "Save File");             Fos.close ();    }


1.2.2. Results

Aa.propertiest

#\u4fdd\u5b58\u6587\u4ef6

#Sun Jan 14:06:25 CST 2015

Name=\u5f20\u4e09

Aa=123

bb=456

cc=789

1.3. Modify the properties configuration file 1.3.1. Pre-modified configuration file

Test.properties

Age=22

password= Password

Name= Zhang San

1.3.2. Modifying a program's code

Propertiestest.java

  /**     * Changes to configuration file     * @throws IOException *    /@Test public    Void Update () throws ioexception{             Properties Properties = new properties ();             File File = new file ("Test.properties");             FileInputStream fis = new FileInputStream (file);             FileOutputStream fos = new FileOutputStream (file);             Properties.load (FIS);             Properties.setproperty ("name", "John Doe");       Properties.setproperty ("Age", "222");       Properties.setproperty ("Age3", "222");       Properties.store (FOS, "update config file");             Fos.close ();             Fis.close ();          }

1.3.3. modified file contents

Test.properties

#\u66f4\u65b0\u914d\u7f6e\u6587\u4ef6

#Sun Jan 15:20:40 CST 2015

age=222

Name=\u674e\u56db

password=\u5bc6\u7801

age3=222

1.4. Delete 1.4.1 for the properties profile. Delete the contents of the pre-configuration file

Test.properties

#\u66f4\u65b0\u914d\u7f6e\u6587\u4ef6

#Sun Jan 15:20:40 CST 2015

age=222

Name=\u674e\u56db

password=\u5bc6\u7801

age3=222

1.4.2. Removing code from a program

Propertiestest.java

/** * Delete key in config file * @throws ioexception * */@Test public void Delete () throws ioexception{Pr             Operties Properties = new properties ();             File File = new file ("Test.properties");                    FileInputStream fis = new FileInputStream (file);                Properties.load (FIS);             Must first use MAP to save all the content first, or delete, the original content has not map<string, string> map = new hashmap<string, string> ();             set<object> KeySet = Properties.keyset ();             System.out.println (Keyset.size ());           For (object Object:keyset) {string key = (string) Object;           String value = (string) properties.get (key);           System.out.println (key+ "=" +value);       Map.put (key, value);       }//Delete key Map.Remove ("Age3");             Properties.remove ("Age3"); For (java.util.map.entry<string, string> entry:map.entrySet ()) {Properties.setproperty (Entry.getkey (), E      Ntry.getvalue ()); } fileoutputstream fos = new FileOutputStream (file);             Properties.store (Fos, "delete key:age3 in config file");             Fos.close ();          Fis.close ();   }


1.4.3. Post-deletion configuration file contents

Test.properties

#\u5220\u9664\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684key:age3

#Sun Jan 15:23:58 CST 2015

age=222

Name=\u674e\u56db

password=\u5bc6\u7801

1.5. Complete program code

Propertietest.java

Import Java.io.file;import java.io.fileinputstream;import java.io.fileoutputstream;import java.io.IOException; Import Java.util.hashmap;import java.util.map;import java.util.properties;import java.util.Set;  Import Org.junit.Test; public class Propertiestest {public static void main (string[] args) {}/** * Delete key in configuration file * @throws IO Exception * */@Test public void Delete () throws ioexception{Properties Properties = New Properties ()             ;             File File = new file ("Test.properties");                    FileInputStream fis = new FileInputStream (file);                Properties.load (FIS);             Must first use MAP to save all the content first, or delete, the original content has not map<string, string> map = new hashmap<string, string> ();             set<object> KeySet = Properties.keyset ();             System.out.println (Keyset.size ());           For (object Object:keyset) {string key = (string) Object; String value = (string) properties.get (key);          System.out.println (key+ "=" +value);       Map.put (key, value);       }//Delete key Map.Remove ("Age3");             Properties.remove ("Age3"); For (java.util.map.entry<string, string> entry:map.entrySet ()) {Properties.setproperty (Entry.getkey (), E       Ntry.getvalue ());       } fileoutputstream fos = new FileOutputStream (file);             Properties.store (Fos, "delete key:age3 in config file");             Fos.close ();          Fis.close ();             }/** * Changes to configuration file * @throws IOException */@Test public void Update () throws ioexception{             Properties Properties = new properties ();             File File = new file ("Test.properties");                         FileInputStream fis = new FileInputStream (file);             Properties.load (FIS);             Must first use MAP to save all the content first, or an update, the original content has not map<string, string> Map = new hashmap<string, string> ();     set<object> KeySet = Properties.keyset ();        System.out.println (Keyset.size ());           For (object Object:keyset) {string key = (string) Object;           String value = (string) properties.get (key);           System.out.println (key+ "=" +value);       Map.put (key, value);       } map.put ("Name", "John Doe");       Map.put ("Age", "222");                   Map.put ("Age3", "222"); For (java.util.map.entry<string, string> entry:map.entrySet ()) {Properties.setproperty (Entry.getkey (), E       Ntry.getvalue ());             } fileoutputstream fos = new FileOutputStream (file);             Properties.store (FOS, "update config file");             Fos.close ();          Fis.close ();       /** * Write content to config file * @throws IOException */@Test public void Write () throws IOException {             Properties Properties = new properties ();       String file = "Aa.properties";             FileOutputStream fos = new FileOutputStream (file); Properties.setproperty ("AA", "123");      Properties.setproperty ("BB", "456");       Properties.setproperty ("CC", "789");                   Properties.setproperty ("name", "Zhang San");             Properties.store (FOS, "Save File");    Fos.close (); }/** * Read config file * @throws IOException */@Test public void Read () throws ioexception{Prop             Erties Properties = new properties ();             File File = new file ("Test.properties");             FileInputStream fis = new FileInputStream (file);             Properties.load (FIS);             set<object> KeySet = Properties.keyset (); for (Object object:keyset) {System.out.println (object.tostring () + "=" +properties.getproperty (object.tostring ()       )); }    } }


Java read and write operations to properties configuration files

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.