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

≡放荡痞女 提交于 2019-12-21 12:51:08

问题


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?


回答1:


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.




回答2:


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

}



回答3:


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



来源:https://stackoverflow.com/questions/1913565/how-can-i-access-a-txt-file-in-a-jar-with-fileinputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!