[英]Use Absolute path for ClassLoader getResourceAsStream()
I am trying to use ClassLoader getResourceAsStream() 我正在尝试使用ClassLoader getResourceAsStream()
My Direcory structure is like below: 我的Direcory结构如下:
Project1
-src
-main
-java
-webapp
-WEB-INF
-MYLOC
-someprops.properties
For classloader.getResourceAsStream("MYLOC/someprops.properties")
works fine. 对于
classloader.getResourceAsStream("MYLOC/someprops.properties")
工作正常。
But now I have to move the properties file outside of the .war, like in C:\\someprops.properties
但现在我必须将属性文件移到.war之外,就像在
C:\\someprops.properties
But, classloader.getResourceAsStream("C:\\someprops.properties")
does not work. 但是,
classloader.getResourceAsStream("C:\\someprops.properties")
不起作用。 Can it not use an absolute path? 它可以不使用绝对路径吗?
If you have a native file path then you don't need to use getResourceAsStream
, just create a FileInputStream
in the normal way. 如果您有本机文件路径,那么您不需要使用
getResourceAsStream
,只需以正常方式创建FileInputStream
。
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) (如果文件很大,您可能希望将
FileInputStream
包装在BufferedInputStream
)
The method classloader.getResourceAsStream
looks up resources on the classpath. 方法
classloader.getResourceAsStream
在类路径上查找资源。 If you want to load your someprops.properties
file with classloader.getResourceAsStream
then add it to your classpath. 如果要使用
classloader.getResourceAsStream
someprops.properties
文件,请将其添加到类路径中。 Otherwise, if this is a properties file, you could always use the Properties.load method. 否则,如果这是属性文件,则始终可以使用Properties.load方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.