Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 943
topless Avatar asked Dec 16 '09 09:12

topless


People also ask

How do I read text from FileInputStream?

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.

How do I view files in a jar file?

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.

How can I access a folder inside of a resource folder from inside my jar file?

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.

How do you specify a file path in FileInputStream?

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


2 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.

like image 81
skaffman Avatar answered Sep 24 '22 17:09

skaffman


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

}
like image 29
ZZ Coder Avatar answered Sep 25 '22 17:09

ZZ Coder