InputStream from relative path

Allan Jiang picture Allan Jiang · Oct 7, 2011 · Viewed 93.1k times · Source

I have a relative file path (for example "/res/example.xls") and I would like to get an InputStream Object of that file from that path.

I checked the JavaDoc and did not find a constructor or method to get such an InputStream from a path/

Anyone has any idea? Please let me know!

Thank you

Answer

Tomasz Nurkiewicz picture Tomasz Nurkiewicz · Oct 7, 2011

Use FileInputStream:

InputStream is = new FileInputStream("/res/example.xls");

But never read from raw file input stream as this is terribly slow. Wrap it with buffering decorator first:

new BufferedInputStream(is);

BTW leading slash means that the path is absolute, not relative.