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

Java中加载配置文件的集中方法

2016-02-19 15:12 507 查看
第一种方法:用io流实现配置文件的加载

这种方法配置文件需要放在工程项目下跟目录下

<pre name="code" class="java"><pre name="code" class="java">
/** 从配置文件总读取配置内容 **/
InputStream ips=new FileInputStream("config.properties");
Properties props=new Properties();
props.load(ips);
/** 从配置文件中读取相应的值 **/
String classname=props.getProperty("classname");






第二种方法:使用类加载器实现配置文件的加载

这种方法配置文件需要放在classpath指定的目录下

/** 使用类加载器的方式加载配置文件 **/
InputStream ips=HashTest.class.getClassLoader().getResourceAsStream("com/bbp/test/config.properties");

/** 使用类内部的getResourceAsStream实现 **/
InputStream ips=HashTest.class.getResourceAsStream("config.properties");
InputStream ips=HashTest.class.getResourceAsStream("resouces/config.properties");
InputStream ips=HashTest.class.getResourceAsStream("/com/bbp/test/resouces/config.properties");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: