How can I access a txt file in a jar with FileInputStream?

topless picture topless · Dec 16, 2009 · Viewed 10.8k times · Source

I am aware of the getResourceAsStream() method but there is an issue with the parser that reads the file, the whole structure was implemented to expect a FileInputStream() and the getResourceAsStream() returns an input stream which cannot be casted. Is there any easy "fix" for this situation?

Answer

skaffman picture skaffman · Dec 16, 2009

A resource contained within a JAR file is not itself a file, and cannot be read using a FileInputStream. If you have code that absolutely requires a FileInputStream, then you'll need to extract the data using getResourceAsStream(), copy it into a temporary file, then pass a FileInputStream for that temporary file to your code.

Of course, in future, never write code to expect concrete implementations of things like InputStream, you'll always regret it.