Properties 类表示了一个持久的属性集,它是 Hashtable 的子类。增加了将Hashtable对象中的关键字和值保存到文件和从文件中读取关键字和值到Hashtable对象中的方法。如果要用Properties.store方法存储Properties对象中的内容,每个属性的关键字和值必须是String类型。关于Properties 的API可以查看 http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/util/Properties.html。
A.用6种方法读取Properties文件
1。使用java.util.Properties类的load()方法示例:InputStreamin=lnewBufferedInputStream(newFileInputStream(name));Propertiesp=newProperties();p.load(in);
2。使用java.util.ResourceBundle类的getBundle()方法示例:ResourceBundlerb=ResourceBundle.getBundle(name,Locale.getDefault());
3。使用java.util.PropertyResourceBundle类的构造函数示例:InputStreamin=newBufferedInputStream(newFileInputStream(name));ResourceBundlerb=newPropertyResourceBundle(in);
4。使用class变量的getResourceAsStream()方法示例:InputStreamin=JProperties.class.getResourceAsStream(name);Propertiesp=newProperties();p.load(in);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法示例:InputStreamin=JProperties.class.getClassLoader().getResourceAsStream(name);Propertiesp=newProperties();p.load(in);
6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法示例:InputStreamin=ClassLoader.getSystemResourceAsStream(name);Propertiesp=newProperties();p.load(in);
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法示例:InputStreamin=context.getResourceAsStream(path);Propertiesp=newProperties();p.load(in);
B.操作Properties文件的源代码
/*
操作属性文件,可以为我们的程序带来更方便的移植性,下面是一个示例,可以读、写、更改属性
读采用了两种方式,一种是采用
Properties
类,另外一种是采用资源绑定类
ResourceBundle
类,
下面是源程序,里面有详细的注释:
*/
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.util.Properties;
import
java.util.ResourceBundle;
/**
*
对属性文件(
xx.properties
)的操作
*
注:属性文件一定要放在当前工程的根目录下,也就是放在与
src
目录在同一个目录下(我的
JDevelop
*
是这样的)
*/
public
class
OperatePropertiesFile {
public
OperatePropertiesFile() {
}
/**
*
采用
Properties
类取得属性文件对应值
*
@param
propertiesFileName
properties
文件名,如
a.properties
*
@param
propertyName
属性名
*
@return
根据属性名得到的属性值,如没有返回
""
*/
private
String getValueByPropertyName(String propertiesFileName,String propertyName) {
String s=
""
;
Properties p=
new
Properties();
//
加载属性文件读取类
FileInputStream in;
try
{
//propertiesFileName
如
test.properties
in =
new
FileInputStream(propertiesFileName);
//
以流的形式读入属性文件
p.load(in);
//
属性文件将该流加入的可被读取的属性中
in.close();
//
读完了关闭
s=p.getProperty(propertyName);
//
取得对应的属性值
}
catch
(Exception e) {
e.printStackTrace();
}
return
s;
}
/**
*
采用
ResourceBundel
类取得属性文件对应值,这个只能够读取,不可以更改及写新的属性
*
@param
propertiesFileNameWithoutPostfix
properties
文件名,不带后缀
*
@param
propertyName
属性名
*
@return
根据属性名得到的属性值,如没有返回
""
*/
private
String getValueByPropertyName_(String propertiesFileNameWithoutPostfix,String propertyName) {
String s=
""
;
//
如属性文件是
test.properties
,那此时
propertiesFileNameWithoutPostfix
的值就是
test
ResourceBundle bundel = ResourceBundle.getBundle(propertiesFileNameWithoutPostfix);
s=bundel.getString(propertyName);
return
s;
}
/**
*
更改属性文件的值,如果对应的属性不存在,则自动增加该属性
*
@param
propertiesFileName
properties
文件名,如
a.properties
*
@param
propertyName
属性名
*
@param
propertyValue
将属性名更改成该属性值
*
@return
是否操作成功
*/
private
boolean
changeValueByPropertyName(String propertiesFileName,String propertyName,String propertyValue) {
boolean
writeOK=
true
;
Properties p=
new
Properties();
FileInputStream in;
try
{
in =
new
FileInputStream(propertiesFileName);
p.load(in);
//
in.close();
p.setProperty(propertyName,propertyValue);
//
设置属性值,如不属性不存在新建
//p.setProperty("testProperty","testPropertyValue");
FileOutputStream out=
new
FileOutputStream(propertiesFileName);
//
输出流
p.store(out,
"Just Test"
);
//
设置属性头,如不想设置,请把后面一个用
""
替换掉
out.flush();
//
清空缓存,写入磁盘
out.close();
//
关闭输出流
}
catch
(Exception e) {
e.printStackTrace();
}
return
writeOK;
}
//根据key读取value
public static String readValue(String filePath,String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
String value = props.getProperty (key);
System.out.println(key+value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//读取properties的全部信息
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty (key);
System.out.println(key+Property);
}
} catch (Exception e) {
e.printStackTrace();
}
} //写入properties信息
public static void writeProperties(String filePath,String parameterName,String parameterValue) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
prop.load(fis);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "Update '" + parameterName + "' value");
} catch (IOException e) {
System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
}
}
public
static
void
main(String[] args) {
OperatePropertiesFile operatePropertiesFile =
new
OperatePropertiesFile();
operatePropertiesFile.changeValueByPropertyName(
"db.properties"
,
"DBLocation"
,
"D://Palfinger//palfinger.mdb"
);
}
}
假如有一个属性文件
db.properties如下:
DBLocation=D/://Palfinger//palfinger.mdb
dbul=jdbcracle:thin:@202.16.147.104:1521ub
userid=user
password=password
jdbc=oracle.jdbc.driver.OracleDriver
public static String readValue(String filePath,String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
String value = props.getProperty (key);
System.out.println(key+value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//读取properties的全部信息
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty (key);
System.out.println(key+Property);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//写入properties信息
public static void writeProperties(String filePath,String parameterName,String parameterValue) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
prop.load(fis);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "Update '" + parameterName + "' value");
} catch (IOException e) {
System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
}
}
userid=user
password=password
jdbc=oracle.jdbc.driver.OracleDriver