properties文件简介及其常用Java操作

一、properties文件简介

java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"(推荐)或"键:值"的格式,在properties文件中,可以用"#"(推荐)或者"//"来作注释,properties文件在Java编程中用到的地方很多,操作很方便。(注意:其实properties文件相当于只是定义了一种文件格式,在实际应用中后缀名可以改为其他任何符合后缀名规范的名称,比如.cfg,.ini等后缀名)

现在定义一个databaseInfo.properties文件,如下:
#################################
#   财务系统配置文件
#   2014年11月11日
#################################
#   Oracle驱动
driver=oracle.jdbc.driver.OracleDriver
#   Oralc数据库地址
url=jdbc:oracle:thin:@192.168.2.3:1521:orcl
#   用户名
username=cwdb
#   密码
password=cwdb
#################################

注意事项:
当我们在properties文件中编辑中文字符后,会提示无法保存的问题,由于myEclipse中properties资源文件的默认编码格式是ISO-8859-1,此时就需要修改默认的编码格式,打开myEclipse的Window->Perferences->General->ContentTypes,找到Text->Java Properties File选中,将下面的Default encoding修改为:UTF-8,然后点击右边的Update按钮,最后点击OK按钮即可,如图:

 

二、Properties类的重要方法
Properties 类存在于包Java.util中,该类继承自 Hashtable (即:public class java.util.Properties extends java.util.Hashtable{...})
1. getProperty( String  key) ,用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load( InputStream  inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 databaseInfo.properties 文件)进行装载来获取该文件中的所有键-值对。以供 getProperty ( String  key) 来搜索。

3. setProperty( String  key, String  value),调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置键-值对。
4. store( OutputStream  out, String  comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

5. clear(),清除所有装载的键-值对。该方法在基类中提供。

三、Java对Properites类的常用操作

 1 import java.io.FileOutputStream;
 2 import java.io.IOException;
 3 import java.io.InputStream;
 4 import java.io.OutputStream;
 5 import java.util.Iterator;
 6 import java.util.Properties;
 7 import java.util.Set;
 8 
 9 /**
10  * @Description:Java对Properites类的常用操作
11  */
12 public class PropDemo {
13 
14     // 根据key读取value
15     public String readValue(String filePath, String key) {
16         Properties props = new Properties();
17         try {
18             InputStream in = this.getClass().getResourceAsStream("/"+filePath);
19             props.load(in);
20             String value = props.getProperty(key);
21             System.out.println(key + "=" + value);
22             
23             in.close();
24             return value;
25         } catch (Exception e) {
26             e.printStackTrace();
27             return null;
28         }
29     }
30 
31     // 写入properties信息
32     public void writeProperties(String filePath, String parameterName, String parameterValue) {
33         Properties prop = new Properties();
34         try {
35             InputStream fis = this.getClass().getResourceAsStream("/"+filePath);
36             // 从输入流中读取属性列表(键值对)
37             prop.load(fis);
38             
39             OutputStream fos=new FileOutputStream(this.getClass().getResource("/"+filePath).getPath());
40             // 设置新的键值对
41             prop.setProperty(parameterName, parameterValue);
42             // 将此 Properties 表中的属性列表(键和元素对)写入输出流
43             prop.store(fos, "add key: '" + parameterName + "' value:" + parameterValue);
44             
45             fis.close();
46             fos.close();
47         } catch (IOException e) {
48             e.printStackTrace();
49         }
50     }
51     
52     // 读取properties的全部信息
53     public void readProperties(String filePath) {
54         Properties props = new Properties();
55         try {
56             InputStream in = this.getClass().getResourceAsStream("/"+filePath);
57             props.load(in);
58             
59             // 使用Set集合取得所有key值
60             Set keyValue = props.keySet();
61             // 使用while循环遍历
62             Iterator it = keyValue.iterator(); 
63             while (it.hasNext()){
64                 String key = (String) it.next();
65                 String Property = props.getProperty(key);
66                 System.out.println(key + "=" + Property);
67             }
68             in.close();
69         } catch (Exception e) {
70             e.printStackTrace();
71         }
72     }
73 
74     public static void main(String[] args) {
75         
76         // 根据key读取value
77         new PropDemo().readValue("databaseInfo.properties", "url");
78         
79         // 写入properties信息
80         new PropDemo().writeProperties("databaseInfo.properties", "databaseType", "oracle");
81         
82         // 读取properties的全部信息
83         new PropDemo().readProperties("databaseInfo.properties");
84     }
85 }

注意事项:
java的properties文件一般放到classpath下面,这样程序能方便读取到,有关classpath实际上就是java类或者库的存放路径,即java字节码.class文件的存放路径。在java工程中,properties与class文件放到一块。在web工程中,最简单的方法是放到web应用的WEB- INFclasses目录下即可,也可以放在其他文件夹下面。

原文地址:https://www.cnblogs.com/cnjavahome/p/4225150.html