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

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

问题描述

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



我使用以下代码来读取文件,但是我收到这个错误:

 错误:java.io.FileNotFoundException:staedteliste .txt(没有这样的文件或目录)






  public class Test {

ArrayList< String []>值;

public static void main(String [] args){
// TODO自动生成的方法存根

URL url = Test.class.getClassLoader()。的getResource( SRC / MJB / staedteliste.txt);
System.out.println(url.getPath()); //我在这里得到一个nullpointerexception
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应用;通常是一些配置文件或静态资源,只读)。



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



Java为您提供 classLoader.getResource() 方法。而Eclipse(在正常设置中)假设 src / 是在你的类路径的根目录,所以编译后,它将所有东西复制到你的输出目录( bin / ),编译形式的java文件( .class ),其余的就是。

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



编辑:此外,如果您的文件在概念上与java类绑定(例如,一些 com.example.MyClass 有一个 MyClass.cfg 关联的配置文件),您可以使用 getResource()方法,并使用(资源)相对路径: MyClass.getResource( MyClass.cfg)。然后,该文件将在类路径中进行搜索,但将该类包预先附加。所以在这种情况下,你通常会把你的 MyClass.cfg MyClass.java 目录。


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);
    }
}

}

解决方案

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).

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 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.

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.

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天全站免登陆