[关闭]
@Cesar 2016-03-29T05:32:14.000000Z 字数 680 阅读 1295

properties文件操作

Java 学习

案例

  1. public static void main(String[] args) {
  2. Properties properties = new Properties();
  3. try {
  4. properties.load(TestProperties.class.getClassLoader().getResourceAsStream("properties.properties"));
  5. } catch (IOException e) {
  6. e.printStackTrace();
  7. return;
  8. }
  9. String a = properties.getProperty("cesar");
  10. System.out.println(a);
  11. }

获取properties文件的方式

  1. 通过绝对路径:InputStream is=new FileInputStream(filePath)
  2. 通过Class.getResourceAsStream(path)
    Class.getResourceAsStream(String path):path不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由 ClassLoader获取资源。
  3. 通过ClassLoader.getResourceAsStream(path) Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path不能以’/'开头,最终是由ClassLoader获取资源。
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注