Java 使用 Intellij idea IDE 读取文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26949985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading files with Intellij idea IDE
提问by ashimashi
I am a long time eclipse user and I have started to play around with IntelliJ IDEA.
我是 eclipse 的长期用户,我已经开始使用 IntelliJ IDEA。
So from my understanding, a project in IntelliJ is the same as the Eclipse workspace. Additionally, a module in IntelliJ is the equivalent of a project in Eclipse.
所以根据我的理解,IntelliJ 中的一个项目和 Eclipse 工作区是一样的。此外,IntelliJ 中的模块相当于 Eclipse 中的项目。
I created a project in IntelliJ, but I'm still not sure why there is a src
folder in it if it is supposed to be a workspace.
我在 IntelliJ 中创建了一个项目,但src
如果它应该是一个工作区,我仍然不确定为什么其中有一个文件夹。
Afterwards, I created a module in the project and a class inside the src
directory of the new module with this code:
之后,我在项目中创建了一个模块,并src
使用以下代码在新模块的目录中创建了一个类:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Hello World!");
Scanner input = new Scanner(new File ("test123.txt"));
String answer = input.nextLine();
System.out.println(answer);
}
}
The problem is that I get an error trying to read the file. I tried putting the .txt file inside my src file which is located inside the module and outside the src
directory but inside the module. But in both cases the file is not found. Yes the code works, I tried it on Eclipse and it works fine. The file name is spelled correctly as well.
问题是我在尝试读取文件时出错。我尝试将 .txt 文件放在我的 src 文件中,该文件位于模块内部和src
目录外部,但位于模块内部。但是在这两种情况下都找不到文件。是的,代码有效,我在 Eclipse 上尝试过,效果很好。文件名也拼写正确。
Here is a picture of my project/workspace if it is helpful:
如果有帮助,这是我的项目/工作区的图片:
采纳答案by azalut
Just move the file directly to the project folder which calls Java(and something under the blue-blurred stripe you made :P).
只需将文件直接移动到调用Java的项目文件夹(以及您制作的蓝色模糊条纹下的内容:P)。
If that doesn't help, then move the test123.txt
file to FirstJavaProgram
directory.
如果这没有帮助,则将test123.txt
文件移动到FirstJavaProgram
目录。
You can also change filename to one of these:
您还可以将文件名更改为以下之一:
src/test123.txt
FirstJavaProgram/src/test123.txt
src/test123.txt
FirstJavaProgram/src/test123.txt
I am not sure which one will be fine in your case.
我不确定哪一种适合你的情况。
回答by Makoto
Use the full path of the file instead.
请改用文件的完整路径。
Right click on your file in your project, select "Copy Path", and paste that in to the path of your file.
右键单击项目中的文件,选择“复制路径”,然后将其粘贴到文件路径中。
EDIT: You could also use a relative path for your file. If your file is located in resources/
, then you could use the form ./resources/yourfile.txt
.
编辑:您还可以为您的文件使用相对路径。如果您的文件位于 中resources/
,那么您可以使用./resources/yourfile.txt
.
├── resources
│?? └── test123.txt
└── src
├── MainClass.java
回答by Mourad MAMASSI
create directory "files" make all files inside this directory. Select the directory and click right select -> Mark Directory As -> Resources Root :)
创建目录“文件”使该目录中的所有文件。选择目录并单击右键选择-> 将目录标记为-> 资源根:)
回答by Chisx
Instructions
指示
- Simply right-click on your project directory (the very top folder), then select "New > Directory". Specify a name such as "resources".
- 只需右键单击您的项目目录(最顶层的文件夹),然后选择“新建 > 目录”。指定一个名称,例如“资源”。
- (optional) Once the folder is generated then right-click "resources" directory, then select "New > Directory" and create an "images" directory.
- Drag-and-drop your image file into the "images" or "resources"
folder you just created.
Reference your newly added project file with code
File myFile = new File("./resources/images/newfile.txt");
- (可选)文件夹生成后,右键单击“资源”目录,然后选择“新建 > 目录”并创建“图像”目录。
- 将您的图像文件拖放到您刚刚创建的“图像”或“资源”文件夹中。使用代码引用新添加的项目文件
File myFile = new File("./resources/images/newfile.txt");
回答by kevin
回答by Raining
Run
>Edit Configurations
- Under
Configuration
tabWorking directory
: Your file path is relative to this.
Run
>Edit Configurations
- 在
Configuration
选项卡下Working directory
: 你的文件路径是相对于此的。
Hope this help!
希望这有帮助!
回答by Yash Paunikar
u need to specify complete path inside quotes something like this-->
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Application {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("E:\coding\ProcessingFiles\src\myfile.txt");
Scanner input = new Scanner(file);
while (input.hasNextLine()){
String line = input.nextLine();
System.out.println(line);
}
input.close();
}
}