如何将文件作为资源读入`RandomAccessFile`?

时间:2015-11-16 02:43:20

标签: java maven

我有一个jar文件,其中包含/dict/文件夹中的一堆文件。 所以我把它添加为maven依赖。然后,为了为文件创建RandomAccessFile,我将其作为资源读取,放入File,然后将其提供给RandomAccessFile构造函数。以下是完成工作的方式:

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();
}

这是输出:

----> 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)

我注意到的一个问题是using File(...) is not a good idea for reading resources

但是现在我不确定在没有File作为中间步骤的情况下,我们最好阅读整个过程。

1 个答案:

答案 0 :(得分:1)

URL#getFile没有按照您的想法行事,您应该阅读JavaDocs以了解它的作用。

相反,您应该使用类似URL#openStream之类的内容,并将内容自行写入物理File

粗略的例子......

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();
}

您可能会发现某些使用的File.createTempFile