繁体   English   中英

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

[英]How to read files as resource into `RandomAccessFile`?

我有一个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)

我注意到的一个问题是, 使用File(...)并不是读取资源的好主意

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

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有一定用途

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM