分享

Java Properties 类读取和修改配置文件信息(转)

 芥纳须弥 2015-12-16
在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。比如说我们开发了一个操作数据库的模块,在开发的时候我们连接本地的数据库那么 IP ,数据库名称,表名称,数据库主机等信息是我们本地的,要使得这个操作数据的模块具有通用性,那么以上信息就不能写死在程序里。通常我们的做法是用配置文件来解决。各种语言都有自己所支持的配置文件类型。比如 Python ,他支持 .ini 文件。因为他内部有一个nfigParser 类来支持 .ini 文件的读写,根据该类提供的方法程序员可以自由的来操作 .ini 文件。而在 Java 中, Java 支持的是 .properties 文件的读写。 JDK 内置的java.util.Properties 类为我们操作 .properties 文件提供了便利。

使用J2SE API读取Properties文件的六种方法

1。使用java.util.Properties类的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);

2。使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

3。使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);

4。使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);

补充


Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);


以下为服务器、数据库信息

dbPort = localhost

databaseName = mydb

dbUserName = root

dbPassword = root

# 以下为数据库表信息

dbTable = mytable

# 以下为服务器信息

ip = 192.168.0.9

······

在上面的文件中我们假设该文件名为: test.properties 文件。其中 # 开始的一行为注释信息;在等号“ = ”左边的我们称之为 key ;等号“ = ”右边的我们称之为 value 。(其实就是我们常说的键 - 值对) key 应该是我们程序中的变量。而 value是我们根据实际情况配置的。

以下是操作properties文件的工具类:

Java代码 
  1. import java.io.BufferedInputStream;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.util.Properties;  
  9.   
  10. /** 
  11.  * Java读写修改Property文件 
  12.  * @author xiewanzhi 
  13.  * @date 2011-4-7上午09:19:03 
  14.  * @version 1.0 
  15.  */  
  16. public class PropertiesConfig {  
  17.   
  18.     /** 
  19.      * 根据KEY,读取文件对应的值 
  20.      * @param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties 
  21.      * @param key 键 
  22.      * @return key对应的值 
  23.      */  
  24.     public static String readData(String filePath, String key) {  
  25.         //获取绝对路径  
  26.         filePath = PropertiesConfig.class.getResource("/" + filePath).toString();  
  27.         //截掉路径的”file:“前缀  
  28.         filePath = filePath.substring(6);  
  29.         Properties props = new Properties();  
  30.         try {  
  31.             InputStream in = new BufferedInputStream(new FileInputStream(filePath));  
  32.             props.load(in);  
  33.             in.close();  
  34.             String value = props.getProperty(key);  
  35.             return value;  
  36.         } catch (Exception e) {  
  37.             e.printStackTrace();  
  38.             return null;  
  39.         }  
  40.     }  
  41.     /** 
  42.      * 修改或添加键值对 如果key存在,修改, 反之,添加。 
  43.      * @param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties 
  44.      * @param key 键 
  45.      * @param value 键对应的值 
  46.      */  
  47.     public static void writeData(String filePath, String key, String value) {  
  48.         //获取绝对路径  
  49.         filePath = PropertiesConfig.class.getResource("/" + filePath).toString();  
  50.         //截掉路径的”file:/“前缀  
  51.         filePath = filePath.substring(6);  
  52.         Properties prop = new Properties();  
  53.         try {  
  54.             File file = new File(filePath);  
  55.             if (!file.exists())  
  56.                 file.createNewFile();  
  57.             InputStream fis = new FileInputStream(file);  
  58.             prop.load(fis);  
  59.             //一定要在修改值之前关闭fis  
  60.             fis.close();  
  61.             OutputStream fos = new FileOutputStream(filePath);  
  62.             prop.setProperty(key, value);  
  63.             //保存,并加入注释  
  64.             prop.store(fos, "Update '" + key + "' value");  
  65.             fos.close();  
  66.         } catch (IOException e) {  
  67.             System.err.println("Visit " + filePath + " for updating " + value + " value error");  
  68.         }  
  69.     }  
  70.       
  71.     public static void main(String[] args) {  
  72.         System.out.println(PropertiesConfig.readData("com/xiewanzhi/property/config.properties""port"));  
  73. //      PropertiesConfig.writeData("com/xiewanzhi/property/config.properties", "port", "12345");  
  74.     }  
  75. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多