您的位置:首页 > 移动开发

Java Application下读取properties配置文件

2013-11-27 08:40 405 查看
在java应用程序开发中,经常需要读取配置文件来获取系统参数或配置信息。配置文件可以使用xml格式文件,在java中存在.properties文件专门用作配置文件使用。在java中,类Properties用于处理配置文件相关的读取。下面是一个关于根据所提供的键获取值的示例。

public static String getvalue(String key)
{
Properties p=new Properties();
FileInputStream fis;
String url = new File("").getAbsolutePath() + File.separator+  "config.properties"; // 获取位于工程根目录下的config.properties配置文件绝对路径
try {
fis = new FileInputStream(url);
p.load(fis);
fis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.print("problems with properties");
e.printStackTrace();
}
return p.getProperty(key);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: