[英]How to read files as resource into `RandomAccessFile`?
I have a jar file which contains a bunch of files in /dict/
folder. 我有一个jar文件,其中包含
/dict/
文件夹中的一堆文件。 So I added it as a maven dependency. 因此,我将其添加为maven依赖项。 Then in order to create
RandomAccessFile
for a file, I read it as a resource, put into File
, and then give it to the RandomAccessFile
constructor. 然后,为了为文件创建
RandomAccessFile
,我将其作为资源读取,放入File
,然后将其提供给RandomAccessFile
构造函数。 Here is how things are done: 这是完成的过程:
URL resourceURL = getClass().getResource("/dict/index.verb" );
System.out.println("----> file " + resourceURL.getFile());
File f = new File(resourceURL.getFile());
System.out.println("Can read = " + f.canRead());
try {
RandomAccessFile _file = new RandomAccessFile(f, "r");
System.out.println(_file.length());
} catch (java.io.IOException e) {
e.printStackTrace();
}
Here is the output: 这是输出:
----> modified file file:/Users/i-danielk/.m2/repository/edu/illinois/cs/cogcomp/wordnet/1.0/wordnet-1.0.jar!/dict/index.verb
Can read = false
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
java.io.FileNotFoundException: file:/Users/i-danielk/.m2/repository/edu/illinois/cs/cogcomp/wordnet/1.0/wordnet-1.0.jar!/dict/index.verb (No such file or directory)
at java.io.RandomAccessFile.open0(Native Method)
An issue I noticed is that, using File(...)
is not a good idea for reading resources . 我注意到的一个问题是, 使用
File(...)
并不是读取资源的好主意 。
But now I am not sure what is the best we to read the do the whole procedure without having File
as an intermediate step. 但是现在我不确定在没有
File
作为中间步骤的情况下,阅读整个过程的最佳方法是什么。
URL#getFile
isn't doing what you think it is and you should read the JavaDocs to find out what it does do. URL#getFile
并没有按照您的想象做,您应该阅读JavaDocs来了解它的作用。
Instead, you should use something like URL#openStream
and write the contents out to a physical File
yourself. 相反,您应该使用类似
URL#openStream
的内容,然后将内容自己写到物理File
。
As a rough example... 作为一个粗略的例子...
URL resourceURL = getClass().getResource("/dict/index.verb");
File output = new File("some file somewhere");
try (InputStream is = resourceURL.openStream(); OutputStream os = new FileOutputStream(output)) {
byte[] buffer = new byte[2048];
int bytesRead = -1;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (IOException exp) {
exp.printStackTrace();
}
You might find File.createTempFile
of some use 您可能会发现
File.createTempFile
有一定用途
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.