用Java读取文件(使用IDE)

发布于 2024-10-31 20:25:38 字数 581 浏览 3 评论 0原文

我试图编写一段 Java 代码来读取文件中的每一行并将其添加到 Arraylist 中。这是我的代码:

// try catch block removed for clarity.
file = new TextFileIn("names.txt");
String line = null;
while ((line = file.readLine()) != null) {
    list.add(line);
}

names.txt 与我的代码位于同一包文件夹中。我使用的IDE是Eclipse。然而,当我运行代码时,我收到了 FileNotFoundExceptionTextFileIn 类来自 http:// www.javaranch.com/doc/com/javaranch/common/TextFileIn.html

我怎样才能找到该文件?

I was trying to write a piece of Java code to read each line from a file and add it to an Arraylist. Here is my code:

// try catch block removed for clarity.
file = new TextFileIn("names.txt");
String line = null;
while ((line = file.readLine()) != null) {
    list.add(line);
}

names.txt was located in the same package folder as my code. The IDE I used was Eclipse. When I run the code however I got a FileNotFoundException. The TextFileIn class comes from http://www.javaranch.com/doc/com/javaranch/common/TextFileIn.html

How could I get the file to be found?

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

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

发布评论

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

评论(3

温柔少女心 2024-11-07 20:25:38

访问文件时您依赖当前工作目录。工作目录取决于您启动应用程序的方式。在 Eclipse 中,它通常是项目根文件夹。在命令行中它是当前打开的目录。您无法从 Java 代码内部控制这一点,因此对于应该是应用程序本身一部分的文件来说,这被认为是一种不好的做法。

该文件显然与正在运行的代码位于同一个包中,因此它已经在类路径中,然后您可以(并且应该)直接从类路径中获取它。

InputStream input = getClass().getResourceAsStream("names.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
String line = null;

while ((line = reader.readLine()) != null) {
    // ...
}

如果您在static上下文中调用此函数,则将getClass()替换为YourClassName.class,其中YourClassName是此代码所在的类的名称。

另请参阅:

You're relying on the current working directory when accessing the file. The working directory depends on how you started the application. In Eclipse it's usually the project root folder. In commandline it's the currently opened directory. You cannot control this from inside the Java code, so this is considered a poor practice for files which are supposed to be part of the application itself.

The file is apparently in the same package as the running code, thus it's already in the classpath and then you could (and should) just get it straight from the classpath.

InputStream input = getClass().getResourceAsStream("names.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
String line = null;

while ((line = reader.readLine()) != null) {
    // ...
}

If you're calling this in static context, then replace getClass() by YourClassName.class where YourClassName is the name of the class where this code is sitting in.

See also:

叹梦 2024-11-07 20:25:38

它位于包文件夹中。
“names.txt”引用项目根目录。因此,您必须在源文件夹中进行搜索,例如“src/your.package/names.txt”。

It is located in the package folder.
"names.txt" is referencing to the project root. So you have to search in your source folder like "src/your.package/names.txt".

生活了然无味 2024-11-07 20:25:38

您应该学习如何从命令行编译和运行程序,以及如何更改 IDE 的设置,以实现相同的目的。基础知识是你可以随身携带的东西,从 IDE 到 IDE,从 eclipse 到 netbeans,再到 IntelliJ...。

即使您通过单击 或 从 IDE 从桌面运行,您实际上也位于操作系统的文件夹中。在 Eclipse 中,运行代码时默认是您的classes-dir,但可以在 run as 选项卡之一进行修改,它是 ${PROJECT}/bin 或 ${PROJECT}/课程,无论您的课程在哪里。因此,您必须将文件放在那里,但请注意 - 如果您按“全部清除”或其他操作,它可能会被删除,因此请使用副本。

为什么不使用 java.util.Scanner 而不是有趣的 TextFileIn?

Scanner scanner = new Scanner ("names.txt");
while (scanner.hasNextLine ())
    String line = scanner.nextLine ();

You should lern how to compile and run programs from the command line, and later, how to change the settings of your IDE, to do the same. The basic knowledge is something you can carry with you from IDE to IDE, from eclipse to netbeans, to IntelliJ... .

Even if you're running from the desktop by clicking or from an IDE, you're sitting virtually in a folder of your OS. In eclipse, it is by default your classes-dir when running the code, but can be modified in one of the run as tabs, and it is ${PROJECT}/bin or ${PROJECT}/classes, wherever your classes go. So you have to place your file there, but attention - it might get deleted if you press 'clean all' or something, so use a copy.

Instead of funny TextFileIn, why don't you use java.util.Scanner?

Scanner scanner = new Scanner ("names.txt");
while (scanner.hasNextLine ())
    String line = scanner.nextLine ();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文