Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InputStream from relative path

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!

like image 576
Allan Jiang Avatar asked Oct 07 '11 19:10

Allan Jiang


People also ask

How to read File Path from inputStream in Java?

ClassLoader classLoader = getClass(). getClassLoader(); InputStream inputStream = classLoader. getResourceAsStream("fileTest. txt"); String data = readFromInputStream(inputStream);

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

What is Java inputStream file?

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 .


1 Answers

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.

like image 56
Tomasz Nurkiewicz Avatar answered Sep 23 '22 13:09

Tomasz Nurkiewicz