java對properties設定檔的讀寫操作,properties設定檔

來源:互聯網
上載者:User

java對properties設定檔的讀寫操作,properties設定檔
1.1.  對properties設定檔的讀取1.1.1.  要讀取的檔案

設定檔key和value之間用冒號[:]和等號[=]都是可以的.

Test.properties

name:\u5F20\u4E09

password:\u5BC6\u7801

age:22

1.1.2.  讀取的程式碼

PropertiesTest.java

/**     * 讀取設定檔     * @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.  結果

後台列印結果

age:22

password:密碼

name:張三

1.2.  對propertiest設定檔的寫入操作1.2.1.  寫入程式的代碼

PropertiesTest.java

/**     * 向設定檔寫入內容     * @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", "張三");                   properties.store(fos, "儲存檔案");             fos.close();    }


1.2.2.  結果

aa.propertiest

#\u4FDD\u5B58\u6587\u4EF6

#Sun Jan 25 14:06:25 CST 2015

name=\u5F20\u4E09

aa=123

bb=456

cc=789

1.3.  對properties設定檔的修改操作1.3.1.  修改前的設定檔

Test.properties

Age=22

Password=密碼

Name=張三

1.3.2.  修改程式的代碼

PropertiesTest.java

  

  /**     * 對設定檔的修改     * @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", "李四");       properties.setProperty("age", "222");       properties.setProperty("age3", "222");       properties.store(fos, "更新設定檔");             fos.close();             fis.close();          }

1.3.3.  修改後的檔案內容

Test.properties

#\u66F4\u65B0\u914D\u7F6E\u6587\u4EF6

#Sun Jan 25 15:20:40 CST 2015

age=222

name=\u674E\u56DB

password=\u5BC6\u7801

age3=222

1.4.  對properties設定檔的刪除1.4.1.  刪除前設定檔的內容

Test.properties

#\u66F4\u65B0\u914D\u7F6E\u6587\u4EF6

#Sun Jan 25 15:20:40 CST 2015

age=222

name=\u674E\u56DB

password=\u5BC6\u7801

age3=222

1.4.2.  刪除程式的代碼

PropertiesTest.java

/**     * 刪除設定檔中的key     * @throws IOException     *     */    @Test    public void delete() throws IOException{       Properties properties = new Properties();             File file = new File("test.properties");             FileInputStream fis = new FileInputStream(file);                    properties.load(fis);                //必須先用map將所有的內容先儲存,不然一刪除,原來的內容都沒了       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);       }       //刪除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, "刪除設定檔中的key:age3");             fos.close();             fis.close();          }  


1.4.3.  刪除後的設定檔內容

Test.properties

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

#Sun Jan 25 15:23:58 CST 2015

age=222

name=\u674E\u56DB

password=\u5BC6\u7801

1.5.  完整程式碼

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) {     }    /**     * 刪除設定檔中的key     * @throws IOException     *     */    @Test    public void delete() throws IOException{       Properties properties = new Properties();             File file = new File("test.properties");             FileInputStream fis = new FileInputStream(file);                    properties.load(fis);                //必須先用map將所有的內容先儲存,不然一刪除,原來的內容都沒了       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);       }       //刪除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, "刪除設定檔中的key:age3");             fos.close();             fis.close();          }       /**     * 對設定檔的修改     * @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);             //必須先用map將所有的內容先儲存,不然一更新,原來的內容都沒了       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", "李四");       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, "更新設定檔");             fos.close();             fis.close();          }          /**     * 向設定檔寫入內容     * @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", "張三");                   properties.store(fos, "儲存檔案");             fos.close();    }          /**     * 讀取設定檔     * @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()));       }    } }


 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.