用Java读取文件(使用IDE)
我试图编写一段 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。然而,当我运行代码时,我收到了 FileNotFoundException
。 TextFileIn
类来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
访问文件时您依赖当前工作目录。工作目录取决于您启动应用程序的方式。在 Eclipse 中,它通常是项目根文件夹。在命令行中它是当前打开的目录。您无法从 Java 代码内部控制这一点,因此对于应该是应用程序本身一部分的文件来说,这被认为是一种不好的做法。
该文件显然与正在运行的代码位于同一个包中,因此它已经在类路径中,然后您可以(并且应该)直接从类路径中获取它。
如果您在
static
上下文中调用此函数,则将getClass()
替换为YourClassName.class
,其中YourClassName
是此代码所在的类的名称。另请参阅:
getResourceAsStream()
vs文件输入流
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.
If you're calling this in
static
context, then replacegetClass()
byYourClassName.class
whereYourClassName
is the name of the class where this code is sitting in.See also:
getResourceAsStream()
vsFileInputStream
它位于包文件夹中。
“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".
您应该学习如何从命令行编译和运行程序,以及如何更改 IDE 的设置,以实现相同的目的。基础知识是你可以随身携带的东西,从 IDE 到 IDE,从 eclipse 到 netbeans,再到 IntelliJ...。
即使您通过单击 或 从 IDE 从桌面运行,您实际上也位于操作系统的文件夹中。在 Eclipse 中,运行代码时默认是您的classes-dir,但可以在
run as
选项卡之一进行修改,它是 ${PROJECT}/bin 或 ${PROJECT}/课程,无论您的课程在哪里。因此,您必须将文件放在那里,但请注意 - 如果您按“全部清除”或其他操作,它可能会被删除,因此请使用副本。为什么不使用 java.util.Scanner 而不是有趣的 TextFileIn?
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?