wood burning stoves 2.0
The moose likes Servlets and the fly likes getResourcesAsStream Versus new FileInputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getResourcesAsStream Versus new FileInputStream" Watch "getResourcesAsStream Versus new FileInputStream" New topic

getResourcesAsStream Versus new FileInputStream

Graham VMead
Ranch Hand

Joined: Sep 22, 2003
Posts: 154
Just for my interest, I've seen a lot of posts where people can't get an xml file or say a properties file in a war.

new FileInputStream doesn't work but getClass().getResourceAsStream() does, could someone explain succinctly to me why this is the case. What is the difference between these mechanisms that makes the latter work.

Also I've seen posts where the getResourceAsStream() method works for App Servers that expand the war but not for those that don't, why is this and what is the solution that will work in both expanded and non-expanded cases?

Thanks to anyone who replies

Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
Hi Graham,

Well, the answer is not related so servlets at all, so this topic will probably be moved to I/O. Anyway, the point is that you actually can use a FileInputStream; you just need the complete URL to the file, like "jar:file:/some.war!/your.xml". The tough part -- maybe not that tough, just not trivial -- is to get the war/jar file. Releasing you of the extra effort of getting jar file or finding the exploded path is the purpose of getResourceAsStream() -- as far as I understand it.
I thought that getResourceAsStream() would work for both expanded and non-expanded war files, so I don't know what could work for both. Hope this brief answer clarifies a little, and sorry if it didn't. Best regards

Henrique Sousa<br />SCJP 1.4<br /> <br />All men die, not all men really live - Braveheart, 1995
I agree. Here's the link: http://aspose.com/file-tools
subject: getResourcesAsStream Versus new FileInputStream
It's not a secret anymore!