简体   繁体   English

添加.txt文件作为资源Java

[英]Adding .txt file as a resource Java

I have a .txt file and a image in a file called res. 我有一个.txt文件和一个名为res的文件中的图像。 I added in the file to my path as well. 我在文件中添加了我的路径。 I did the code bellow and it works in my Eclipse IDE just fine. 我做了下面的代码,它在我的Eclipse IDE中运行得很好。 When exporting a jar and running it, it does nothing. 导出jar并运行它时,它什么都不做。 Running the jar with cmd says the class path can not be found. 使用cmd运行jar表示无法找到类路径。 SO I tried the second chunk of code with no success. 所以我尝试了第二块代码没有成功。 My image that is there works fine. 我的形象很好用。 bgi = new ImageIcon(getClass().getResource("bg.png"));

Scanner s = null;
        try {
            s = new Scanner(new File("res//10kaddress.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ArrayList<String> paddress = new ArrayList<String>();
        while (s.hasNext()){
            paddress.add(s.next());
        }
        s.close();

So I tried doing this below and no mater what I do it will not read the txt file 所以我尝试在下面做这个,不管我做什么都不会读取txt文件

URL url = GUI.class.getResource("10kaddress.txt");
    File ff = new File(url.getPath());


    Scanner s = null;
    try {
        s = new Scanner(ff);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    ArrayList<String> paddress = new ArrayList<String>();
    while (s.hasNext()){
        paddress.add(s.next());
    }
    s.close();

and get this error 并得到此错误

java.io.FileNotFoundException: C:\Users\Major%20Lee\Sketch\GUI\res\10kaddress.txt (The system cannot find the path specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at GUI.main(GUI.java:68)
Exception in thread "main" java.lang.NullPointerException
    at GUI.main(GUI.java:73)

Any help would be great. 任何帮助都会很棒。 Thanks! 谢谢!

I would open it as a ResourceStream (or I would just use a ResourceBundle), that is like this - 我会打开它作为ResourceStream(或者我只是使用ResourceBundle),就像这样 -

InputStream is = getClass().getResourceAsStream("10kaddress.txt");
Scanner s = new Scanner(is);

When you have a file embedded in a JAR, you need to use the getResourceAsStream() , as it has been pointed out. 当您在JAR中嵌入了一个文件时,您需要使用getResourceAsStream() ,因为它已被指出。

Also, I suspect that there is a problem in your path. 另外,我怀疑你的路上有问题。 From the error I can see that you are on a Windows machine. 从错误中我可以看到你在Windows机器上。 Your path is : 你的道路是:

res//10kaddress.txt  

When escaping the separators, use a / (single forward slash) or a \\\\ ( double backslash). 转义分隔符时,使用/ (单正斜杠)或\\\\ (双反斜杠)。 The backslash has another backslash with it and is hence called as being escaped. 反斜杠有另一个反斜杠,因此称为转义。 Try changing the path and see if that works for you. 尝试更改路径,看看它是否适合您。

So, make your path as: 所以,让你的道路:

res/10kaddress.txt   

Double-check manually to see if the file is at the path specified. 手动双击以查看文件是否在指定的路径中。

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

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