Java操作Properties文件简介
本章概要
在Java的编程过程中,我们经常需要在项目内或者是外部创建一些配置文件,用以维护一些与项目相关的基本配置。
因此我们经常需要对这些配置文件进行读写的操作。本章主要介绍了几种常见的操作Properties文件的方法。
Properties文件操作简介
Properties文件是我们日常开发中使用最为频繁的配置文件类型之一,对该类型的文件,我们可以使用以下几种方式对其进行读/写的操作:
- 使用自带java.util.ResourceBundle类或者java.util.PropertyResourceBundle类操作Properties文件;
- 使用自带java.util.Properties类操作Properties文件;
- 使用Apache的Commons-Configuration操作Properties文件
ResourceBundle操作Properties文件
由于PropertyResourceBundle类继承自ResourceBundle,两者对Properties文件的操作十分相似,因此这里我们只从ResourceBundle类入手来简单介绍一下其操作Properties文件的用法。
其操作方法可以参见以下的代码:
1 package com.webopenshare.demo.prop.utils; 2 3 import java.util.MissingResourceException; 4 import java.util.ResourceBundle; 5 6 public class PropUtils { 7 8 private static PropUtils _instance = null; 9 10 private ResourceBundle resBundle = null; 11 12 static { 13 _instance = new PropUtils(); 14 } 15 16 private PropUtils() { 17 loadSysConfig("sysConfig"); 18 } 19 20 public static PropUtils getInstance() { 21 return _instance; 22 } 23 24 public ResourceBundle loadSysConfig(final String fileName) { 25 resBundle = ResourceBundle.getBundle(fileName); 26 return resBundle; 27 } 28 29 public String getSysConfig(final String key) { 30 String sysConfig = null; 31 32 try { 33 sysConfig = resBundle.getString(key); 34 } catch (MissingResourceException e) { 35 sysConfig = key; 36 } 37 38 return sysConfig; 39 } 40 }
通过上面的代码,我们可以看到,ResourceBundle类可以很方便的读取Properties文件的配置内容,但是如果我们想要修改配置内容呢?很遗憾,通过查看ResourceBundle类,我们可以看到,它并没有提供任何的方法用于此操作。
java.util.Properties操作Properties文件
java自带了java.util.Properties类,用于读写Properties文件,如下代码所示:
1 package com.webopenshare.demo.prop.utils; 2 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.util.Properties; 6 7 public class PropUtils { 8 9 private static PropUtils _instance = null; 10 11 private Properties props = null; 12 13 private final String CONFIG_FILE = "/sysConfig.properties"; 14 15 static { 16 _instance = new PropUtils(); 17 } 18 19 private PropUtils() { 20 loadSysConfig(CONFIG_FILE); 21 } 22 23 public static PropUtils getInstance() { 24 return _instance; 25 } 26 27 public Properties loadSysConfig(final String fileName) { 28 props = new Properties(); 29 try { 30 props.load(Properties.class.getResourceAsStream(fileName)); 31 } catch (IOException e) { 32 e.printStackTrace(); 33 } 34 return props; 35 } 36 37 public String getSysConfig(final String key) { 38 String sysConfig = props.getProperty(key, key); 39 return sysConfig; 40 } 41 42 public void updateSysConfig(final String key, final String value) { 43 props.setProperty(key, value); 44 } 45 46 public void persistSysConfig() { 47 try { 48 props.store(new FileOutputStream(System.getProperty("user.dir") + "/config" + CONFIG_FILE), "Updated System Configure"); 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } 52 } 53 }
通过上面的示例,我们知道了:
- 通过java.util.Properties的load(inStream)的方法,可以加载Properties文件的配置;
- 通过getProperty(key)或者getProperty(key, defaultValue)的方法可以获取指定key的配置属性;
- 通过setPerperty(key, value)可以插入或者是跟新配置;
- 通过store(outStream, comment)的方法,可以把最终的修改持久化到文件当中
但是,使用java.util.Properties更新或者插入新的配置属性时,我们不能够保有原先文件的配置格式,包括了配置的顺序跟配置的Comments,这并不是最佳的效果,我们希望能够保持配置的Comments,这对我们之后的维护有重要的提示作用。
Apache Commons Configuration操作Properties文件
我们可以通过Apache开源项目,Commons-Configuration来操作Properties文件,使用时需要引入以下的依赖:
- commons-configuration2-${releaseVersion}.jar
- commons-lang3-${releaseVersion}.jar
- commons-logging-${releaseVersion}.jar
1 package com.webopenshare.demo.prop.utils; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileWriter; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 9 import org.apache.commons.configuration2.PropertiesConfiguration; 10 import org.apache.commons.configuration2.PropertiesConfigurationLayout; 11 import org.apache.commons.configuration2.ex.ConfigurationException; 12 13 public class PropUtils { 14 15 private static PropUtils _instance = null; 16 17 private static PropertiesConfiguration config = new PropertiesConfiguration(); 18 19 private static PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); 20 21 private static final String CONFIG_FILE = "C:/Config/sysConfig.properties"; 22 23 static { 24 _instance = new PropUtils(); 25 _instance.loadSysConfig(CONFIG_FILE); 26 } 27 28 private PropUtils() { 29 } 30 31 public static PropUtils getInstance() { 32 return _instance; 33 } 34 35 public void loadSysConfig(final String fileName) { 36 try { 37 layout.load(config, new InputStreamReader(new FileInputStream(fileName))); 38 } catch (FileNotFoundException | ConfigurationException e) { 39 e.printStackTrace(); 40 } 41 } 42 43 public static String getStringConfig(final String key) { 44 return config.getString(key); 45 } 46 47 public static void addOrUpdateStringConfig(final String key, final String value) { 48 config.setProperty(key, value); 49 persistSysConfg(); 50 } 51 52 public static void persistSysConfg() { 53 try { 54 FileWriter out = new FileWriter(CONFIG_FILE, false); 55 layout.save(config, out); 56 } catch (IOException | ConfigurationException e) { 57 e.printStackTrace(); 58 } 59 } 60 }
使用这个开源库,我们可以很方便的来维护配置文件信息,而且可以确保在跟新或者插入新的配置信息时,保持文件的格式不受影响(保留配置的顺序以及配置的Comments)。
我要小额赞助,鼓励作者写出更好的文章: