Use Absolute path for ClassLoader getResourceAsStream()

user3018487 picture user3018487 · Nov 21, 2013 · Viewed 32k times · Source

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?

Answer

Ian Roberts picture Ian Roberts · Nov 21, 2013

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)