如何使用FileInputStream访问jar中的txt文件? [英] How can I access a txt file in a jar with FileInputStream?

查看:255
本文介绍了如何使用FileInputStream访问jar中的txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 getResourceAsStream()方法,但是解析器读取文件存在问题,整个结构实现为期望 FileInputStream() getResourceAsStream()返回无法转换的输入流。这种情况有没有简单的修复?

I am aware of the getResourceAsStream() method but there is an issue with the parser that reads the file, the whole structure was implemented to expect a FileInputStream() and the getResourceAsStream() returns an input stream which cannot be casted. Is there any easy "fix" for this situation?

推荐答案

JAR文件中包含的资源本身不是文件,无法使用 FileInputStream 读取。如果您的代码绝对需要 FileInputStream ,那么您需要使用 getResourceAsStream()提取数据,将其复制到一个临时文件中,然后将该临时文件的 FileInputStream 传递给您的代码。

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.

当然,在将来,永远不要编写代码来期待具体的实现,比如 InputStream ,你总会后悔。

Of course, in future, never write code to expect concrete implementations of things like InputStream, you'll always regret it.

这篇关于如何使用FileInputStream访问jar中的txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆