java 如何使用 FileInputStream 访问 jar 中的 txt 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1913565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I access a txt file in a jar with FileInputStream?
提问by topless
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?
我知道该getResourceAsStream()方法,但是读取文件的解析器存在问题,整个结构被实现为期望 aFileInputStream()并getResourceAsStream()返回一个无法强制转换的输入流。这种情况有什么简单的“修复”吗?
回答by skaffman
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 FileInputStreamfor that temporary file to your code.
JAR 文件中包含的资源本身不是文件,不能使用FileInputStream. 如果您的代码绝对需要FileInputStream,那么您需要使用 提取数据getResourceAsStream(),将其复制到临时文件中,然后FileInputStream将该临时文件的a 传递给您的代码。
Of course, in future, never write code to expect concrete implementations of things like InputStream, you'll always regret it.
当然,在未来,永远不要写代码来期望像 那样的具体实现InputStream,你会永远后悔的。
回答by ZZ Coder
I recently encountered the same issue. A third-party library we use reads from FileInputStream but the resources can be anywhere, in a JAR or remote. We used to write to temporary files but that has too much overhead.
我最近遇到了同样的问题。我们使用的第三方库从 FileInputStream 读取,但资源可以位于 JAR 或远程中的任何位置。我们曾经写入临时文件,但这有太多的开销。
A better solution is write a FileInputStream which wraps InputStream. Here is the class we use,
更好的解决方案是编写一个包装 InputStream 的 FileInputStream。这是我们使用的类,
public class VirtualFileInputStream extends FileInputStream {
private InputStream stream;
public VirtualFileInputStream(InputStream stream) {
super(FileDescriptor.in); // This will never be used
this.stream = stream;
}
public int available() throws IOException {
throw new IllegalStateException("Unimplemented method called");
}
public void close() throws IOException {
stream.close();
}
public boolean equals(Object obj) {
return stream.equals(obj);
}
public FileChannel getChannel() {
throw new IllegalStateException("Unimplemented method called");
}
public int hashCode() {
return stream.hashCode();
}
public void mark(int readlimit) {
stream.mark(readlimit);
}
public boolean markSupported() {
return stream.markSupported();
}
public int read() throws IOException {
return stream.read();
}
public int read(byte[] b, int off, int len) throws IOException {
return stream.read(b, off, len);
}
public int read(byte[] b) throws IOException {
return stream.read(b);
}
public void reset() throws IOException {
stream.reset();
}
public long skip(long n) throws IOException {
return stream.skip(n);
}
public String toString() {
return stream.toString();
}
}
回答by Dapeng
don't believe your parse only works on FileInputStream but not InputStream
不要相信你的解析只适用于 FileInputStream 而不是 InputStream
if that is the real case, and you must use that parser
如果这是真实情况,您必须使用该解析器
2 options
2个选项
use adapter pattern to create a CustomFileInputStream and overwrite the respective methods, more of redirect the getResourceAsStream data to the CustomFileInputStream
save ur getResourceAsStream into a temp file, and parse the temp file, then delete the file when done
使用适配器模式创建一个 CustomFileInputStream 并覆盖相应的方法,更多的是将 getResourceAsStream 数据重定向到 CustomFileInputStream
将您的 getResourceAsStream 保存到临时文件中,并解析临时文件,完成后删除该文件

