InputStream from relative path

一个人想着一个人 提交于 2019-12-22 01:24:31

问题


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


回答1:


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.




回答2:


InputStream inputStream = Files.newInputStream(Path);



回答3:


Initialize a variable like: Path filePath, and then:

FileInputStream fileStream;
try {
    fileStream = new FileInputStream(filePath.toFile());
} catch (Exception e) {
    throw new RuntimeException(e);
}

Done ! Using Path you can have access to many useful methods.




回答4:


new FileInputStream("your_relative_path") will be relative to the current working directory.



来源:https://stackoverflow.com/questions/7692298/inputstream-from-relative-path

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