Java reads and writes the properties configuration file, and the properties configuration file

Source: Internet
Author: User

Java reads and writes the properties configuration file, and the properties configuration file
1.1. Read the properties configuration file 1.1.1. The file to be read

You can use the colon [:] and equal sign [=] between the key and value of the configuration file.

Test. properties

Name: \ u5F20 \ u4E09

Password: \ u5BC6 \ u7801

Age: 22

1.1.2. Read program code

PropertiesTest. java

/*** Read the configuration File * @ throws IOException */@ Test public void read () throws IOException {Properties properties = new Properties (); file File = new File ("test. properties "); FileInputStream FD = new FileInputStream (file); properties. load (FCM); Set <Object> keySet = properties. keySet (); for (Object object: keySet) {System. out. println (object. toString () + ":" + properties. getProperty (object. toString ()));}}


1.1.3. Results

Result printed in the background

Age: 22

Password: password

Name: James

1.2. write operation on the propertiest configuration file 1.2.1. Write Program code

PropertiesTest. java

/*** Write content to the configuration 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. setproperties ("bb", "456"); properties. setProperty ("cc", "789"); properties. setProperty ("name", "Michael"); properties. store (fos, "save files"); fos. close ();}


1.2.2. Result

Aa. propertiest

# \ U4FDD \ u5B58 \ u6587 \ u4EF6

# SunJan25 14:06:25 CST 2015

Name = \ u5F20 \ u4E09

Aa = 123

Bb = 1, 456

Cc = 789

1.3. Modify the properties configuration file 1.3.1. The configuration file before modification

Test. properties

Age = 22

Password = Password

Name = James

1.3.2. modify the program code

PropertiesTest. java

/*** Modify the configuration file * @ throws IOException */@ Test public void update () throws IOException {Properties properties Properties = new Properties (); file file = new File ("test. properties "); FileInputStream FD = new FileInputStream (file); FileOutputStream fos = new FileOutputStream (file); properties. load (FCM); properties. setProperty ("name", "Li Si"); properties. setProperty ("age", "222"); properties. setProperty ("age3", "222"); properties. store (fos, "Update Configuration File"); fos. close (); FCM. close ();}

1.3.3. Modified File Content

Test. properties

# \ U66F4 \ u65B0 \ u914D \ u7F6E \ u6587 \ u4EF6

# SunJan25 15:20:40 CST 2015

Age = 222

Name = \ u674E \ u56DB

Password = \ u5BC6 \ u7801

Age3 = 222

1.4. Delete the properties configuration file 1.4.1. Contents of the configuration file before deletion

Test. properties

# \ U66F4 \ u65B0 \ u914D \ u7F6E \ u6587 \ u4EF6

# SunJan25 15:20:40 CST 2015

Age = 222

Name = \ u674E \ u56DB

Password = \ u5BC6 \ u7801

Age3 = 222

1.4.2. delete program code

PropertiesTest. java

/*** Delete the key in the configuration file * @ throws IOException **/@ Test public void delete () throws IOException {Properties properties = new Properties (); file file = new File ("test. properties "); FileInputStream FD = new FileInputStream (file); properties. load (FCM); // all the content must be saved first with map. Otherwise, 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);} // deletes the key map. remove ("age3"); properties. remove ("age3"); for (java. util. map. entry <String, String> entry: map. entrySet () {properties. setProperty (entry. getKey (), entry. getValue ();} FileOutputStream fos = new FileOutputStream (file); properties. store (fos, "delete key: age3 in the configuration file"); fos. close (); FCM. close ();}


1.4.3. Deleted Configuration File Content

Test. properties

# \ U5220 \ u9664 \ u914D \ u7F6E \ u6587 \ u4EF6 \ u4E2D \ u7684key: age3

# SunJan25 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 the key * @ throws IOException in the configuration file **/@ Test public void delete () throws IOException {Properties properties Properties = new Properties (); file file = new File ("test. properties "); FileInputStream FD = new FileInputStream (file); properties. load (FCM); // all the content must be saved first with map. Otherwise, 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);} // deletes the key map. remove ("age3"); properties. remove ("age3"); for (java. util. map. entry <String, String> entry: map. entrySet () {properties. setProperty (entry. getKey (), entry. getValue ();} FileOutputStream fos = new FileOutputStream (file); properties. store (fos, "delete key: age3 in the configuration file"); fos. close (); FCM. close ();}/*** modify the configuration file * @ throws IOException */@ Test public void update () throws IOException {Properties properties Properties = new Properties (); file file = new File ("test. properties "); FileInputStream FD = new FileInputStream (file); properties. load (FCM); // all the content must be saved first with map. Otherwise, the original content is not updated with 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", "Li Si"); map. put ("age", "222"); map. put ("age3", "222"); for (java. util. map. entry <String, String> entry: map. entrySet () {properties. setProperty (entry. getKey (), entry. getValue ();} FileOutputStream fos = new FileOutputStream (file); properties. store (fos, "Update Configuration File"); fos. close (); FCM. close ();}/*** write content to the configuration file * @ throws IOException */@ Test public void write () throws IOException {Properties properties Properties = new Properties (); string file = "aa. properties "; FileOutputStream fos = new FileOutputStream (file); properties. setProperty ("aa", "123"); properties. setproperties ("bb", "456"); properties. setProperty ("cc", "789"); properties. setProperty ("name", "Michael"); properties. store (fos, "save files"); fos. close ();}/*** read configuration file * @ throws IOException */@ Test public void read () throws IOException {Properties properties Properties = new Properties (); file file = new File ("test. properties "); FileInputStream FD = new FileInputStream (file); properties. load (FCM); Set <Object> keySet = properties. keySet (); for (Object object: keySet) {System. out. println (object. toString () + "=" + properties. getProperty (object. toString ()));}}}


 

 

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.