java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容格式是"键=值",在properties文件中,可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便。
一、Properties类的几个重要方法
Properties类存在于java.util包下,该类继承自Hashtable.
1. getProperty(String key) 用指定的键在此属性列表中搜索属性,也就是通过参数key,找到key所对应的value,如果未找到则返回null。
2. setProperty(String key, String value) 调用Hashtable 的 put方法。返回值是Hashtable 调用put 的结果。用于往.properties文件中添加新的键值对。
3. load(InputStream inStream) 从输入流中读取属性列表(键和元素对)。通过对指定的文件.properties 文件进行装载来获取该文件中的所有键 - 值对。 以供 getProperty(String key) 来搜索。
4. store(OutputStream out, String comments) 以适合使用 load(InputStream inStream)方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear() 清空所有装载的 键 - 值对。该方法在基类Hashtable<K,V>中提供。
二、在程序中如何操作.properties文件
给出一份完整代码,供参考
package com.properties;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
public class TestProperties {
/**
* 通过key读取value的值
*
* @param keyName
* 键的名称
* @param filePath
* 需要读取的文件路径
* @return 键所对应的值
*/
public static Object getKeyInfo(String filePath, String keyName) {
InputStream is = TestProperties.class.getClassLoader()
.getResourceAsStream(filePath);
Properties pro = new Properties();
try {
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
is = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return pro.get(keyName);
}
/**
* 往properties中写入键值对
*
* @param filePath
* 需要写入的文件路径
* @param keyName
* key值
* @param value
* value值
*/
public static void SetProperties(String filePath, String keyName,
String value) {
InputStream is = TestProperties.class.getClassLoader()
.getResourceAsStream(filePath);
OutputStream os = null;
Properties pro = new Properties();
try {
pro.load(is);
os = new FileOutputStream(filePath);
pro.setProperty(keyName, value);
pro.store(os, "Update '" + keyName + "' value");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
os.close();
is = null;
os = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 读取properties文件中的所有键值对
*
* @param filePath
* 文件路径
*/
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream in = TestProperties.class.getClassLoader()
.getResourceAsStream(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();
}
}
public static void main(String[] args) {
TestProperties.getKeyInfo("testPro.properties", "A");
TestProperties.SetProperties("testPro.properties", "C", "33333");
readProperties("src\testPro.properties");
}
}
补充:.properties文件 一般放在src根目录下或自己在src下建一个目录存放,当然放在其他地方可以。
本文介绍了Java中Properties类的基本概念及常用方法,包括如何通过键获取值、如何向.properties文件中添加键值对以及如何读取文件中的所有键值对。提供了完整的示例代码。
726

被折叠的 条评论
为什么被折叠?



