gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:47:24 29 4
gpt4 key购买 nike

我有一个 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 作为中间步骤的情况下阅读整个过程的最佳方式是什么。

最佳答案

URL#getFile 没有按照您的想法进行操作,您应该阅读 JavaDocs找出它的作用。

相反,您应该使用类似 URL#openStream 的内容并自己将内容写入物理文件

作为一个粗略的例子......

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

关于java - 如何将文件作为资源读入 `RandomAccessFile`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33727745/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com