java - 为什么 getClass().getResource 对 RandomAccessFile/ReversedLinesFileReader (Apache Commons) 有问题?

标签 java eclipse io

我的txt文件位于资源文件夹中(父文件夹是项目文件夹),该资源文件夹被设为源文件夹(以便Eclipse自动将其内容复制到/bin根目录)。在资源文件夹中,我有lottery_archives 文件夹和其中的drawArchive_5x36.txt 文件。我做了“清理项目”并再次构建它。我检查过 - 文件存在于/bin/lottery_archives/drawArchive_5x36.txt

下面的代码有什么问题吗?为什么会出现 FileNotFoundException?

顺便说一句,为什么所有这些都与 getClass().getResource 一起跳舞,然后是 URL->String (我需要将 String fileName 提供给 RandomAccessFile 构造函数),我不完全明白为什么我不能提供 String "/lottery_archives/drawArchive_5x36.txt”直接到构造函数?我觉得有些东西可能与jar不同,而不是本地文件,但无法表述清楚。

 import org.apache.commons.io.input.ReversedLinesFileReader;

 public String readLastLine5x36() throws IOException {
    String archiveFileName = "/lottery_archives/drawArchive_5x36.txt";
    URL archiveURL = this.getClass().getResource(archiveFileName);      
    String fileName = archiveURL.toString();        
    File file = new File(fileName);     

    ReversedLinesFileReader reader = new ReversedLinesFileReader(file, Charset.forName("UTF-8"));
    String result = reader.readLine();

archiveURL.toString 返回“file:/L:/MySeriousProjects/JackPotAlert/JackPotAlert/bin/lottery_archives/drawArchive_5x36.txt”

错误堆栈跟踪:

Exception in thread "main" java.io.FileNotFoundException: \lottery_archives\drawArchive_5x36.txt (Системе не удается найти указанный путь)
at java.io.RandomAccessFile.open0(Native Method)
at java.io.RandomAccessFile.open(RandomAccessFile.java:316)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
at org.apache.commons.io.input.ReversedLinesFileReader.<init>(ReversedLinesFileReader.java:135)
at org.apache.commons.io.input.ReversedLinesFileReader.<init>(ReversedLinesFileReader.java:78)
at com.codeuniverse.jackpotalert.domain.Archive.readLastLine5x36(Archive.java:128)
at com.codeuniverse.jackpotalert.domain.Archive.main(Archive.java:139)

尝试将 URL 转换为 URI 并将 URI 提供给 RandomAccessFile 构造函数(通过 apache 类)

    String archiveFileName = "/lottery_archives/drawArchive_5x36.txt";
    URL archiveURL = this.getClass().getResource(archiveFileName);      
    File file = new File(archiveURL.toURI());   

产生 URISyntaxException。

最佳答案

您不能对应用程序 jar 文件中的文件使用 this.getClass().getResource(archiveFileName)。因为它不是一个单独的文件。

您需要使用this.getClass().getResourceAsStream(archiveFileName):

public InputStream getResourceAsStream(String name)

Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String).

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'. Otherwise, the absolute name is of the following form:

    modified_package_name/name 

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

Parameters:

name - name of the desired resource

Returns:

 A InputStream object or null if no resource with this name is found 

Throws:

  NullPointerException - If name is null

Since:

  JDK1.1

请注意,此方法返回一个 InputStream,这可能会限制您读取内容的选项。

关于java - 为什么 getClass().getResource 对 RandomAccessFile/ReversedLinesFileReader (Apache Commons) 有问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49902423/

相关文章:

java - 将图像上传到服务器并将带有文本值的路径存储到 MySQL 中

java - 在java动物猜谜游戏中无法找到或加载主类

java - 如何保存大的html内容

eclipse - 如何在 Eclipse 中从自动完成功能中删除某些类

android - 扩展 ADT Eclipse 插件(Android 开发工具)

io - 内存和 IO 带宽之间有什么区别,我们如何测量它们?

java - setDefaultCloseOperation 改为显示 JFrame

java - 如何在eclipse中提取将字段或常量值转换为参数的方法

c - 如何正确使用 EOF?

linux - 如何在当前或另一个终端窗口中获​​取 at 命令的输出