使用ClassLoader的绝对路径getResourceAsStream()

use*_*487 9 java classpath classloader absolute-path

我正在尝试使用ClassLoader getResourceAsStream()

我的Direcory结构如下:

Project1

 -src
  -main
   -java
  -webapp
   -WEB-INF
-MYLOC
-someprops.properties
Run Code Online (Sandbox Code Playgroud)

对于classloader.getResourceAsStream("MYLOC/someprops.properties")作品的罚款.

但现在我必须将属性文件移到.war之外,就像在 C:\someprops.properties

但是,classloader.getResourceAsStream("C:\someprops.properties")不起作用.它可以不使用绝对路径吗?

Ian*_*rts 18

如果您有本机文件路径,那么您不需要使用getResourceAsStream,只需FileInputStream以正常方式创建.

Properties props = new Properties();
FileInputStream in = new FileInputStream("C:\\someprops.properties");
try {
  props.load(in);
} finally {
  in.close();
}
Run Code Online (Sandbox Code Playgroud)

(如果文件很大,你可能想要包装FileInputStream一个BufferedInputStream)