我想在 Eclipse 中使用的文本文件放在哪里? [英] Where to put a textfile I want to use in eclipse?

查看:25
本文介绍了我想在 Eclipse 中使用的文本文件放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在启动程序时读取文本文件.我正在使用 eclipse 并开始了一个新的 java 项目.在我的项目文件夹中,我得到了src"文件夹和标准的JRE 系统库"+ staedteliste.txt ......我只是不知道将文本文件放在哪里.我真的尝试了我能想到的每个文件夹......我不能使用硬编码"路径,因为文本文件需要包含在我的应用程序中......

I need to read a text file when I start my program. I'm using eclipse and started a new java project. In my project folder I got the "src" folder and the standard "JRE System Library" + staedteliste.txt... I just don't know where to put the text file. I literally tried every folder I could think off....I cannot use a "hard coded" path because the text file needs to be included with my app...

我使用以下代码读取文件,但出现此错误:

I use the following code to read the file, but I get this error:

Error:java.io.FileNotFoundException:staedteliste.txt(No such file or directory)

<小时>

public class Test {

ArrayList<String[]> values;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    URL url = Test.class.getClassLoader().getResource("src/mjb/staedteliste.txt");
    System.out.println(url.getPath()); // I get a nullpointerexception here!
    loadList();
}

public static void loadList() {
    BufferedReader reader;
    String zeile = null;

    try {
        reader = new BufferedReader(new FileReader("src/mjb/staedteliste.txt"));
        zeile = reader.readLine();          

        ArrayList<String[]> values = new ArrayList<String[]>();

        while (zeile != null) {             
            values.add(zeile.split(";"));
            zeile = reader.readLine();
        }
        System.out.println(values.size());
        System.out.println(zeile);

    } catch (IOException e) {
        System.err.println("Error :"+e);
    }
}

}

推荐答案

先问问自己:您的文件是应用程序的内部组件吗?(这通常意味着它被打包在你的 JAR 中,或者 WAR 如果它是一个 web 应用程序;通常,它是一些配置文件或静态资源,只读).

Ask first yourself: Is your file an internal component of your application? (That usually implies that it's packed inside your JAR, or WAR if it is a web-app; typically, it's some configuration file or static resource, read-only).

如果答案是肯定的,则您不想为文件指定绝对路径.但是您既不想使用相对路径(作为您的示例)访问它,因为 Java 假定该路径相对于当前目录".通常,这种情况的首选方法是相对于类路径加载它.

If the answer is yes, you don't want to specify an absolute path for the file. But you neither want to access it with a relative path (as your example), because Java assumes that path is relative to the "current directory". Usually the preferred way for this scenario is to load it relatively from the classpath.

Java 为您提供classLoader.getResource() 方法来执行此操作.Eclipse(在正常设置中)假定 src/ 位于类路径的根目录中,因此在编译后,它会将所有内容复制到您的输出目录( bin/> ), 编译形式的 java 文件 ( .class ),其余部分不变.

Java provides you the classLoader.getResource() method for doing this. And Eclipse (in the normal setup) assumes src/ is to be in the root of your classpath, so that, after compiling, it copies everything to your output directory ( bin/ ), the java files in compiled form ( .class ), the rest as is.

因此,例如,如果您将文件放在 src/Files/myfile.txt 中,它将在编译时复制到 bin/Files/myfile.txt> ;并且,在运行时, bin/ 将在您的类路径(的根)中.因此,通过调用 getResource("/Files/myfile.txt")(在其某些变体中),您将能够读取它.

So, for example, if you place your file in src/Files/myfile.txt, it will be copied at compile time to bin/Files/myfile.txt ; and, at runtime, bin/ will be in (the root of) your classpath. So, by calling getResource("/Files/myfile.txt") (in some of its variants) you will be able to read it.

已编辑:此外,如果您的文件在概念上与 java 类相关联(例如,某些 com.example.MyClass 具有 MyClass.cfg 关联的配置文件),可以使用 getResource() 方法 并使用(资源)相对路径:MyClass.getResource("MyClass.cfg").然后将在类路径中搜索该文件,但预先附加了类包.因此,在这种情况下,您通常会将 MyClass.cfgMyClass.java 文件放在同一目录中.

Edited: Further, if your file is conceptually tied to a java class (eg, some com.example.MyClass has a MyClass.cfg associated configuration file), you can use the getResource() method from the class and use a (resource) relative path: MyClass.getResource("MyClass.cfg"). The file then will be searched in the classpath, but with the class package pre-appended. So that, in this scenario, you'll typically place your MyClass.cfg and MyClass.java files in the same directory.

这篇关于我想在 Eclipse 中使用的文本文件放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆