获取properties配置文件

通过FileInputStream获取

注:FileInputStream是InputStream的子类

1
2
3
4
File file = new File("src/main/resources/spring/test.properties");
Properties p = new Properties();
p.load(new FileInputStream(file));
String word = p.getProperty("word");

上面第三步也可以直接

1
p.load(new FileInputStream("src/main/resources/spring/test.properties"));

路径问题:
这里的src/main/resources/spring/test.properties是相对路径,相对于工程的路径,也可以写绝对路径(加上盘符等等)。
如果写/src/main/resources/spring/test.properties表示工程所在盘符的下的这个路径,而不是工程路径下的
如下:

1
2
3
4
File file = new File("src/main/resources/spring/test.properties");
System.out.println(file.getAbsolutePath());
File file1 = new File("/src/main/resources/spring/test.properties");
System.out.println(file1.getAbsoluteFile());

结果为:

C:\Users\Administrator\IdeaProjects\project\src\main\resources\spring\test.properties
C:\src\main\resources\spring\test.properties

注:

p.load()可以传入的参数是Reader和InputStream

通过ClassLoader类加载器获取

1
2
3
4
InputStream resourceAsStream = a.getClass().getClassLoader().getResourceAsStream("spring/test.properties");
Properties p = new Properties();
p.load(resourceAsStream);
String word = p.getProperty("word");

这个方法是从classpath目录下加载的文件,比较方便


通过currentThread当前线程获取

1
2
3
4
InputStream resourceAsStream1 = Thread.currentThread().getContextClassLoader().getResourceAsStream("spring/test.properties");
Properties p = new Properties();
p.load(resourceAsStream1);
String word = p.getProperty("word");

这个也是通过classpath拿到的文件


注:获取之后一定要try catch finally 关闭流资源

文章作者: C.c
文章链接: https://liquidcat.cc/获取properties配置文件.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Me