0

I am using netbeans; I can't figure out the way we are supposed to structure the project when we want to access a ressource file (e.g. a txt file that contains static info):

Here is a simple example, I have a file called Test.java that reads inside a txt file called myfile.txt

I want to type something like :

public class Test {
    public static void main(String[] args) {
        try{
            File f = new File("myfile.txt");
            Scanner s = new Scanner(f);
            System.out.println(s.next());
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}

which seems reasonnable if the myfile.txt is located in the same directory as the .java file.

But NO it seems that if I type my code like that, the txt file should be at the same level as src/ that is the root of my project. Ok I'll accept that and put the txt there, so I clean and build. Now if I run in netbeans (the green arrow button) it runs fine (even if there is no txt file in my build folder, which seems strange) BUT of course if I try to execute directly the jar in the dist folder (which should be the thing you want to distribute once the project is finished) the program fails since there is no txt folder inside the jar ofr next to it.

Ok so I change my strategy and go for the thing which seemed logical, that is put my txt inside the src directory. When I build it appears in the build directory, and also inside the jar.

BUT the program fails (both within netbeans and outside) since the path to the file is not proper in the new File command. So I could change and type

public class Test {
    public static void main(String[] args) {
        try{
            File f = new File("src/myfile.txt");
            Scanner s = new Scanner(f);
            System.out.println(s.next());
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}

but now of course it won't run outside netbeans because the src folder does not mean anything to the poor .jar file.

I can't find a way to address this supposedly trivial task.

Can you help me?

4

3 回答 3

3

如果您想要 jar 中的文本文件,您可以使用如下所示:

public class Test {
    public static void main(String[] args) {
        try{
            Scanner s = new Scanner(Test.class.getResourceAsStream("/myfile.txt")); // <- in the src folder
            Scanner s2 = new Scanner(Test.class.getResourceAsStream("./myfile.txt")); // <- in the package of your *.java file
            System.out.println(s.next());
            System.out.println(s2.next());
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
于 2013-03-06T18:46:04.093 回答
0

尝试这个:

File对象像这样初始化File f = new File("myfile.txt");

现在不要myfile.txt作为 jar 的一部分导出,而是将其放在同一目录中的 jar 文件旁边。

于 2013-03-06T16:03:21.420 回答
0

首先,通过 netbeans 创建一个新文件只会将文件放入项目文件夹的根目录中。(我仍然不知道如何引用这个位置)。

因此,在创建新文件时右键单击“包”,而不是项目。我创建了一个名为资源的包,并将我的文件放入其中。我使用的解决方案(也应该与 file f = new File 一起使用):

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("resources/file.xml");
于 2015-07-14T05:09:01.717 回答