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

java中的Properties文件操作使用举例

2011-03-02 11:14 726 查看
一、文件样式:



二、config.default.properties中的内容:

weightpath=D:/harrypotter/xml/weight.xml
dicpath=D:/harrypotter/xml/dictionary.xml
dictionarypath=D:/harrypotter/cpv_dataset
CHANGEPI=1
CHANGEPN=1
CHANGEPM=1
CHANGEPIC=1
CHANGEPIP=1
name=zhujiadun


三、操作代码

package com.taobao.jifeng.properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class TestProperties {
public static void main(String[] args) {
Properties properties = new Properties();
InputStream is = null;
is = TestProperties.class.getResourceAsStream("/com/taobao/jifeng/properties/config.default.properties");
try {
properties.load(is);
String key = "name";
System.out.println(TestProperties.getValue(properties, key));

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static String getValue(Properties properties,String key)
{

if(properties.containsKey(key))
{
String value = properties.getProperty(key);
return value;
}
else
{
return "";
}
}

public static void clear(Properties properties)
{
properties.clear();
}

public static void setValue(Properties properties,String key,String value)
{
properties.setProperty(key,value);
}
}


四、输出结果:

zhujiadun
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: