`

读取properties文件

阅读更多
properties文件是java中一种常用的文件格式,一般将配置的信息放入此类文件。
归纳properties读取的方法:
1、绝对路径读取


InputStream is = new BufferedInputStream(new FileInputStream("D:\\t.properties"));
Properties p = new Properties();
p.load(is);
System.out.println(p.get("name"));


2、相对路径读取   根据class变量读取

//当前类的目录 加上"/"找到classpath路径
/*InputStream in = TestProperties.class.getResourceAsStream("/com/my/pro/t.properties");*/

//直接找到classpath路径 不需加"/"
/*InputStream in = TestProperties.class.getClassLoader()
.getResourceAsStream("com/my/pro/t.properties");*/
			
//同上
InputStream in = ClassLoader.getSystemResourceAsStream("com/my/pro/t.properties");
			
Properties p = new Properties();
p.load(in);
System.out.println(p.get("name"));
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics