开发者

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

开发者 https://www.devze.com 2022-12-13 23:40 出处:网络
I am aware of the getResou开发者_Go百科rceAsStream() method but there is an issue with the parser that reads the file, the whole structure was implemented to expect a FileInputStream() and the getReso

I am aware of the getResou开发者_Go百科rceAsStream() 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?


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();
    }

}


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

  1. use adapter pattern to create a CustomFileInputStream and overwrite the respective methods, more of redirect the getResourceAsStream data to the CustomFileInputStream

  2. save ur getResourceAsStream into a temp file, and parse the temp file, then delete the file when done

0

精彩评论

暂无评论...
验证码 换一张
取 消