问题
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