简体   繁体   English

当我的班级和文本文件位于可执行JAR中时,如何编写代码访问文本文件?

[英]How can I write code to access a text file when my class and the text file are in an Executable JAR?

I know this question has been asked many, many times before, but the answers don't seem to help me. 我知道这个问题已经被问过很多次了,但是答案似乎并没有帮助我。 Here is my specific issue: 这是我的具体问题:

I have a class "TaQL" that accesses two text files from within its void main(String args[]) method. 我有一个“ TaQL”类,可以从其void main(String args [])方法中访问两个文本文件。 The two text files are in the same package as this class: package "taql". 这两个文本文件与该类位于同一软件包中:软件包“ taql”。 The following code runs fine within Eclipse (Mars) IDE: 以下代码在Eclipse(Mars)IDE中运行良好:

    String stoplist = TaQL.class.getResource(stoplist).toURI().getPath();
    String input = TaQL.class.getResource(input).toURI().getPath();

When I output these two strings I get the correct path to the resource and the code hums along. 当我输出这两个字符串时,我会获得正确的资源路径,并且代码杂乱无章。

However, when I use this method after packaging everything up in a JAR, both strings return "null". 但是,当我将所有内容打包到JAR中之后使用此方法时,两个字符串都将返回“ null”。

Here is what my code does with each of these text files in the non-JAR version 这是我的代码对非JAR版本中的每个文本文件的处理方式

  1. Get filename string for each text file. 获取每个文本文件的文件名字符串。
  2. create a new File object from one string and a new FileReader object from the other. 从一个字符串创建一个新的File对象,从另一个字符串创建一个新的FileReader对象。
  3. These are fed into a stoplist function and a data input iterator, respectively. 它们分别被馈入非索引字表函数和数据输入迭代器。

What I really need to be able to do is to seamlessly access these two text files once they are all in a JAR and then use some reference to these to create a File and FileReader object. 我真正需要做的就是一旦这两个文本文件都放在JAR中,就可以无缝访问它们,然后使用对它们的某种引用来创建File和FileReader对象。

Here is some diagnostic code I put in my main method. 这是我放入主方法中的一些诊断代码。 It will show what the JAR file is seeing as far as files (note that both input and stoplist are filename strings): 它将显示JAR文件和文件所看到的内容(请注意,输入和非中断列表都是文件名字符串):

    System.out.println("intput argument" + input);

    System.out.println("Resource path"+TaQL.class.getResource(stoplist));

    stoplist = TaQL.class.getResource(stoplist).toURI().getPath();
    input = TaQL.class.getResource(input).toURI().getPath();

    System.out.println("Path"+stoplist);
    System.out.println("Input path" + input);

command line output from running the JAR version of my program (note that this code works perfectly fine when I run from Eclipse) 运行程序的JAR版本的命令行输出(请注意,当我从Eclipse运行时,此代码工作得很好)

在此处输入图片说明

To actually access the bytes of those files that may be in a jar file you need to use Class#getResourceAsStream(stopList). 要实际访问jar文件中可能存在的那些文件的字节,您需要使用Class#getResourceAsStream(stopList)。 The URI will give you a path that cannot be used with File or FileInputStream. URI将为您提供一个不能与File或FileInputStream一起使用的路径。 It's working in your IDE because the IDE has not yet packaged up the files, therefore the #getPath finds an actual file path. 它在您的IDE中有效,因为IDE尚未打包文件,因此#getPath可以找到实际的文件路径。

So try this instead: 因此,请尝试以下操作:

try (Reader inputReader = 
        new InputStreamReader(
            TaQL.class.getResourceAsStream(input))) {

    BufferedReader in = new BufferedReader(inputReader);

    for (String line; (line = in.readLine()) != null;) {
        // do something with the line
    }
}

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

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