Use Absolute path for ClassLoader getResourceAsStream()

谁说我不能喝 提交于 2019-12-22 04:05:25

问题


I am trying to use ClassLoader getResourceAsStream()

My Direcory structure is like below:

Project1

 -src
  -main
   -java
  -webapp
   -WEB-INF
-MYLOC
-someprops.properties

For classloader.getResourceAsStream("MYLOC/someprops.properties") works fine.

But now I have to move the properties file outside of the .war, like in C:\someprops.properties

But, classloader.getResourceAsStream("C:\someprops.properties") does not work. Can it not use an absolute path?


回答1:


If you have a native file path then you don't need to use getResourceAsStream, just create a FileInputStream in the normal way.

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

(you may want to wrap the FileInputStream in a BufferedInputStream if the file is large)




回答2:


The method classloader.getResourceAsStream looks up resources on the classpath. If you want to load your someprops.properties file with classloader.getResourceAsStream then add it to your classpath. Otherwise, if this is a properties file, you could always use the Properties.load method.



来源:https://stackoverflow.com/questions/20127017/use-absolute-path-for-classloader-getresourceasstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!