使用ClassLoader的绝对路径getResourceAsStream() [英] Use Absolute path for ClassLoader getResourceAsStream()

查看:385
本文介绍了使用ClassLoader的绝对路径getResourceAsStream()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ClassLoader getResourceAsStream()

I am trying to use ClassLoader getResourceAsStream()

我的Direcory结构如下所示:

My Direcory structure is like below:

Project1

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

对于 classloader.getResourceAsStream( MYLOC / someprops.properties)工作正常。

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

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

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

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

推荐答案

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

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();
}

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

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

这篇关于使用ClassLoader的绝对路径getResourceAsStream()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆