public class PropertiesFile {
private Properties prop ;
public PropertiesFile()
{
prop = new Properties();
}
public PropertiesFile(String filePath,String propertiesName)
{
prop = new Properties();
try{
FileInputStream inputFile = new FileInputStream(filePath+propertiesName);
prop.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex){
System.out.println("读取属性文件失败,文件路径错误或者文件不存在");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("装载文件失败!");
ex.printStackTrace();
}
}
public PropertiesFile(String propertiesName)
{
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propertiesName);
prop = new Properties();
try {
prop.load(inputStream);
inputStream.close();
}catch (FileNotFoundException ex){
System.out.println("读取属性文件失败,文件路径错误或者文件不存在");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("装载文件失败!");
ex.printStackTrace();
}
}
public String getValue(String key)
{
if(prop.containsKey(key))
{
String value = prop.getProperty(key);
return value ;
}else{
return "";
}
}
}
本文介绍了一个Java类PropertiesFile的设计与实现,该类用于加载并读取属性文件。支持从不同路径加载属性文件,并提供了获取属性值的方法。

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



