Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getResourcesAsStream Versus new FileInputStream

 
Graham VMead
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Graham
 
Henrique Sousa
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic