在哪里放置我想在 Eclipse 中使用的文本文件?

发布于 2024-09-02 04:51:38 字数 1288 浏览 7 评论 0原文

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

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

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

别挽留 2024-09-09 04:51:38

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

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

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

因此,例如,如果您将文件放在 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.cfgMyClass.java 文件放在同一目录中。

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.

蝶舞 2024-09-09 04:51:38

采取的一种方法是将

  1. 您正在使用的文件添加到类路径
  2. 使用资源加载器来定位文件:

     URL url = Test.class.getClassLoader().getResource("myfile.txt");
        System.out.println(url.getPath());
        ...
    
  3. 打开它

One path to take is to

  1. Add the file you're working with to the classpath
  2. Use the resource loader to locate the file:

        URL url = Test.class.getClassLoader().getResource("myfile.txt");
        System.out.println(url.getPath());
        ...
    
  3. Open it
铜锣湾横着走 2024-09-09 04:51:38

假设您在 Eclipse 上有一个名为“TestProject”的项目,并且您的工作区文件夹位于 E:/eclipse/workspace。当您构建 Eclipse 项目时,您的类路径是 e:/eclipse/workspace/TestProject。当您尝试读取“staedteliste.txt”时,您正在尝试访问位于 e:/eclipse/workspace/TestProject/staedteliste.txt 的文件。

如果您想为项目创建一个单独的文件夹,请在 TestProject 下创建 Files 文件夹,然后使用(相对路径)/Files/staedteliste.txt 访问该文件。如果您将该文件放在 src 文件夹下,则必须使用 /src/staedteliste.txt 访问它。 src 文件夹内的 Files 文件夹将为 /src/Files/staedteliste.txt

您可以通过添加 e:/eclipse/workspace/< 来使用绝对路径,而不是使用相对路径/code> 开头,但使用相对路径更好,因为只要项目文件夹结构相同,您就可以移动项目而不必担心重构。

Suppose you have a project called "TestProject" on Eclipse and your workspace folder is located at E:/eclipse/workspace. When you build an Eclipse project, your classpath is then e:/eclipse/workspace/TestProject. When you try to read "staedteliste.txt", you're trying to access the file at e:/eclipse/workspace/TestProject/staedteliste.txt.

If you want to have a separate folder for your project, then create the Files folder under TestProject and then access the file with (the relative path) /Files/staedteliste.txt. If you put the file under the src folder, then you have to access it using /src/staedteliste.txt. A Files folder inside the src folder would be /src/Files/staedteliste.txt

Instead of using the the relative path you can use the absolute one by adding e:/eclipse/workspace/ at the beginning, but using the relative path is better because you can move the project without worrying about refactoring as long as the project folder structure is the same.

疾风者 2024-09-09 04:51:38

只需在 src 下创建一个文件夹 Files 并将文件放在那里即可。
这看起来像 src/Files/myFile.txt

注意:
在您的代码中,您需要像这样指定 Files/myFile.txt
例如,

getResource("Files/myFile.txt");

因此当您构建项目并运行 .jar 文件时,这应该能够工作。

Just create a folder Files under src and put your file there.
This will look like src/Files/myFile.txt

Note:
In your code you need to specify like this Files/myFile.txt
e.g.

getResource("Files/myFile.txt");

So when you build your project and run the .jar file this should be able to work.

夜深人未静 2024-09-09 04:51:38

根据您的 Java 类包名称,目录结构可能位于 4 或 5 级以下。

例如,如果您的 Java 类包是 com.stackoverflow.project,那么您的类位于 src/com/stackoverflow/project。

您可以使用多个 ../ 向上移动目录结构,也可以将文本文件移动到与您的类相同的包中。移动文本文件会更容易。

Depending on your Java class package name, you're probably 4 or 5 levels down the directory structure.

If your Java class package is, for example, com.stackoverflow.project, then your class is located at src/com/stackoverflow/project.

You can either move up the directory structure with multiple ../, or you can move the text file to the same package as your class. It would be easier to move the text file.

溺渁∝ 2024-09-09 04:51:38

MJB

请尝试这个

  1. 在 eclipse 中“右键单击”您想要使用的文本文件,
    查看并复制存储在 HDD 中的完整路径(如果在 UNIX 中为“/home/sjaisawal/Space-11.4-template/provisioning/devenv/Test/src/testpath/testfile.txt”)

  2. 输入此完整路径并尝试.

  3. 如果它有效,那么类路径问题,否则 GOK :)

MJB

Please try this

  1. In eclipse "Right click" on the text file u wanna use,
    see and copy the complete path stored in HDD like (if in UNIX "/home/sjaisawal/Space-11.4-template/provisioning/devenv/Test/src/testpath/testfile.txt")

  2. put this complete path and try.

  3. if it works then class-path issue else GOK :)

吃素的狼 2024-09-09 04:51:38

如果这是一个简单的项目,您应该能够将 txt 文件直接拖到项目文件夹中。具体来说,“项目文件夹”将是最高级别的文件夹。我尝试通过将 txt 文件放入 src 文件夹中来做到这一点(对于我正在做的作业项目),但这不起作用。但最后我想办法把它放在项目文件中。

一个很好的教程是 http://www.vogella.com/articles/JavaIO/article .html。我用它作为 I/O 的介绍,它很有帮助。

If this is a simple project, you should be able to drag the txt file right into the project folder. Specifically, the "project folder" would be the highest level folder. I tried to do this (for a homework project that I'm doing) by putting the txt file in the src folder, but that didn't work. But finally I figured out to put it in the project file.

A good tutorial for this is http://www.vogella.com/articles/JavaIO/article.html. I used this as an intro to i/o and it helped.

零崎曲识 2024-09-09 04:51:38

看看这个视频

您所要做的就是选择您的文件(假设它与 txt 文件的简单形式相同),然后将其拖到 Eclipse 中的项目中,然后将其放在那里。选择复制而不是链接,因为它更灵活。就是这样 - 我刚刚尝试过。

Take a look at this video

All what you have to do is to select your file (assuming it's same simple form of txt file), then drag it to the project in Eclipse and then drop it there. Choose Copy instead of Link as it's more flexible. That's it - I just tried that.

莫言歌 2024-09-09 04:51:38

您可能应该看看 ClassLoader 类中 getResource 的各种风格: https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html

You should probably take a look at the various flavours of getResource in the ClassLoader class: https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文