从 JAR 存档中获取 RandomAccessFile
Get RandomAccessFile from JAR archive
总结:
我有一个程序想作为单个 jar
文件发布。
它依赖于三个二进制格式的大资源文件(每个700MB)。可以通过索引轻松访问文件内容,因此我的解析器将这些文件读取为 RandomAccessFile
-objects.
所以我的目标是通过 File
个对象从 jar
访问资源文件。
我的问题:
从我的文件系统访问资源文件时,没有问题,但我打算将它们打包到程序的jar
文件中,因此用户不需要自己处理这些文件。
到目前为止,我发现访问打包在 jar
中的文件的唯一方法是通过 InputStream
(由 class.getResourceAsStream()
生成),这对我的应用程序完全没用,因为它从头到尾阅读这些文件而不是使用 RandomAccessFile
.
会太慢
出于同样的原因,将文件内容复制到一个文件中,读取它并在运行时删除它是不可行的选择。
有人可以确认没有办法实现我的目标或提供解决方案(或提示以便我自己解决)吗?
到目前为止我发现了什么:
我找到了 this 答案,如果我理解答案,它说没有办法解决我的问题:
Resources in a .jar file are not files in the sense that the OS can access them directly via normal file access APIs.
And since java.io.File represents exactly that kind of file (i.e. a thing that looks like a file to the OS), it can't be used to refer to anything in a .jar file.
A possible workaround is to extract the resource to a temporary file and refer to that with a File.
我想我可以理解它背后的原因,但它现在已经八岁多了,虽然我在文件系统和归档方面受的教育不是很多,但我知道 Java 语言有从那以后发展了很多,所以也许有希望? :)
可能无用的背景信息:
这些文件是 2bit
格式的基因组,我通过包装器 class TwoBitFacade
? 使用 biojava
中的 TwoBitParser
。 Javadocs
可以找到 here and here.
资源不是文件,它们存在于 JAR 文件中,该文件不是随机访问介质。
总结:
我有一个程序想作为单个 jar
文件发布。
它依赖于三个二进制格式的大资源文件(每个700MB)。可以通过索引轻松访问文件内容,因此我的解析器将这些文件读取为 RandomAccessFile
-objects.
所以我的目标是通过 File
个对象从 jar
访问资源文件。
我的问题:
从我的文件系统访问资源文件时,没有问题,但我打算将它们打包到程序的jar
文件中,因此用户不需要自己处理这些文件。
到目前为止,我发现访问打包在 jar
中的文件的唯一方法是通过 InputStream
(由 class.getResourceAsStream()
生成),这对我的应用程序完全没用,因为它从头到尾阅读这些文件而不是使用 RandomAccessFile
.
出于同样的原因,将文件内容复制到一个文件中,读取它并在运行时删除它是不可行的选择。
有人可以确认没有办法实现我的目标或提供解决方案(或提示以便我自己解决)吗?
到目前为止我发现了什么:
我找到了 this 答案,如果我理解答案,它说没有办法解决我的问题:
Resources in a .jar file are not files in the sense that the OS can access them directly via normal file access APIs.
And since java.io.File represents exactly that kind of file (i.e. a thing that looks like a file to the OS), it can't be used to refer to anything in a .jar file.
A possible workaround is to extract the resource to a temporary file and refer to that with a File.
我想我可以理解它背后的原因,但它现在已经八岁多了,虽然我在文件系统和归档方面受的教育不是很多,但我知道 Java 语言有从那以后发展了很多,所以也许有希望? :)
可能无用的背景信息:
这些文件是 2bit
格式的基因组,我通过包装器 class TwoBitFacade
? 使用 biojava
中的 TwoBitParser
。 Javadocs
可以找到 here and here.
资源不是文件,它们存在于 JAR 文件中,该文件不是随机访问介质。