FileInputStream如何正确使用Jar包中的资源文件

基本思路

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.

-- <cite>StackOverFlow</cite>

翻译:
由于包含在Jar包中的资源文件本身不是一个文件,所以FileInputStream不能够直接读取Jar中文件的内容,而如果没有其他选择必须使用FileInputStream(比如你使用的第三方库就是使用FileInputStream,需要你传递文件路径)的情况下,你可以考虑通过getResourceAsStream()先获取文件流,然后创建一个系统临时文件,把临时文件传递给FileInputStream使用。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容