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?
FileInputStream reads bytes with the following read methods : read(byte[] b) — reads up to b. length bytes of data from this input stream into an array of bytes. read(byte[] b, int off, int len) — reads up to len bytes of data from this input stream into an array of bytes.
JAR files are packaged in the ZIP file format. The unzip command is a commonly used utility for working with ZIP files from the Linux command-line. Thanks to the unzip command, we can view the content of a JAR file without the JDK.
What you could do is to use getResourceAsStream() method with the directory path, and the input Stream will have all the files name from that dir. After that you can concat the dir path with each file name and call getResourceAsStream for each file in a loop.
This String should contain the path in the file system to where the file to read is located. Here is a code example: String path = "C:\\user\\data\\thefile. txt"; FileInputStream fileInputStream = new FileInputStream(path);
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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With