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!
ClassLoader classLoader = getClass(). getClassLoader(); InputStream inputStream = classLoader. getResourceAsStream("fileTest. txt"); String data = readFromInputStream(inputStream);
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 FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader .
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.
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