您的位置:首页 > 编程语言 > Java开发

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

2015-01-15 00:10 501 查看
一、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类的常用操作

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

/**
* @Description:Java对Properites类的常用操作
*/
public class PropDemo {

// 根据key读取value
public String readValue(String filePath, String key) {
Properties props = new Properties();
try {
InputStream in = this.getClass().getResourceAsStream("/"+filePath);
props.load(in);
String value = props.getProperty(key);
System.out.println(key + "=" + value);

in.close();
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

// 写入properties信息
public void writeProperties(String filePath, String parameterName, String parameterValue) {
Properties prop = new Properties();
try {
InputStream fis = this.getClass().getResourceAsStream("/"+filePath);
// 从输入流中读取属性列表(键值对)
prop.load(fis);

OutputStream fos=new FileOutputStream(this.getClass().getResource("/"+filePath).getPath());
// 设置新的键值对
prop.setProperty(parameterName, parameterValue);
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "add key: '" + parameterName + "' value:" + parameterValue);

fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

// 读取properties的全部信息
public void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream in = this.getClass().getResourceAsStream("/"+filePath);
props.load(in);

// 使用Set集合取得所有key值
Set keyValue = props.keySet();
// 使用while循环遍历
Iterator it = keyValue.iterator();
while (it.hasNext()){
String key = (String) it.next();
String Property = props.getProperty(key);
System.out.println(key + "=" + Property);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {

// 根据key读取value
new PropDemo().readValue("databaseInfo.properties", "url");

// 写入properties信息
new PropDemo().writeProperties("databaseInfo.properties", "databaseType", "oracle");

// 读取properties的全部信息
new PropDemo().readProperties("databaseInfo.properties");
}
}


注意事项:
java的properties文件一般放到classpath下面,这样程序能方便读取到,有关classpath实际上就是java类或者库的存放路径,即java字节码.class文件的存放路径。在java工程中,properties与class文件放到一块。在web工程中,最简单的方法是放到web应用的WEB- INF\classes目录下即可,也可以放在其他文件夹下面。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: