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?
-
1This would be why it's a good idea to code to the interface and not the implementation. – Powerlord Dec 16 '09 at 15:31
-
You shouldn't cast input streams to their implementations. Their are thought for implementing pipe&filters. It is, one (file)inputsrteam reads from a file, another decompress (GZipI.S.), another breaks lines (bufferedI.S.), another... and so on. So if your program expects a stream of bytes expect an inputstream (any I.S.) and then provide some of its implementations. If you want to transform the stream concatenate one with another: new GZipInputStream(new FileInputStream(path)) by example. – helios Jan 07 '10 at 12:12
3 Answers
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.

- 398,947
- 96
- 818
- 769
I recently encountered the same issue. A third-party library we use reads from FileInputStream but the resources can be anywhere, in a JAR or remote. We used to write to temporary files but that has too much overhead.
A better solution is write a FileInputStream which wraps InputStream. Here is the class we use,
public class VirtualFileInputStream extends FileInputStream {
private InputStream stream;
public VirtualFileInputStream(InputStream stream) {
super(FileDescriptor.in); // This will never be used
this.stream = stream;
}
public int available() throws IOException {
throw new IllegalStateException("Unimplemented method called");
}
public void close() throws IOException {
stream.close();
}
public boolean equals(Object obj) {
return stream.equals(obj);
}
public FileChannel getChannel() {
throw new IllegalStateException("Unimplemented method called");
}
public int hashCode() {
return stream.hashCode();
}
public void mark(int readlimit) {
stream.mark(readlimit);
}
public boolean markSupported() {
return stream.markSupported();
}
public int read() throws IOException {
return stream.read();
}
public int read(byte[] b, int off, int len) throws IOException {
return stream.read(b, off, len);
}
public int read(byte[] b) throws IOException {
return stream.read(b);
}
public void reset() throws IOException {
stream.reset();
}
public long skip(long n) throws IOException {
return stream.skip(n);
}
public String toString() {
return stream.toString();
}
}

- 74,484
- 29
- 137
- 169
-
1+1 but I'd call it FakeFileInputStream to underline the need of faking things :) and encourage to not using the concrete FileInputStream implementation. – helios Jan 07 '10 at 12:14
don't believe your parse only works on FileInputStream but not InputStream
if that is the real case, and you must use that parser
2 options
use adapter pattern to create a CustomFileInputStream and overwrite the respective methods, more of redirect the getResourceAsStream data to the CustomFileInputStream
save ur getResourceAsStream into a temp file, and parse the temp file, then delete the file when done

- 1,704
- 13
- 25