简体   繁体   中英

Reading files with Intellij idea IDE

I am a long time eclipse user and I have started to play around with 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.

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.

Afterwards, I created a module in the project and a class inside the src directory of the new module with this code:

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.

Here is a picture of my project/workspace if it is helpful:

IntelliJ 的屏幕截图

Just move the file directly to the project folder which calls Java (and something under the blue-blurred stripe you made :P).

If that doesn't help, then move the test123.txt file to FirstJavaProgram directory.

You can also change filename to one of these:

  1. src/test123.txt

  2. FirstJavaProgram/src/test123.txt

I am not sure which one will be fine in your case.

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
│   └── test123.txt
└── src
    ├── MainClass.java

Yesterday I met the same trouble as you.

change the setting of IDEA. run->Edit Configurations -> (select your Application at left window.then set the input content named "Working directory" at right window)

单击此处查看图像

create directory "files" make all files inside this directory. Select the directory and click right select -> Mark Directory As -> Resources Root :)

Instructions

  1. Simply right-click on your project directory (the very top folder), then select "New > Directory". Specify a name such as "resources".

在此处输入图片说明

  1. (optional) Once the folder is generated then right-click "resources" directory, then select "New > Directory" and create an "images" directory.
  2. 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");
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();

    }
}
  1. Run > Edit Configurations
  2. Under Configuration tab
    • Working directory : Your file path is relative to this.

Hope this help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM