Java – read files using the IntelliJ IDE

I've been an eclipse user for a long time, and I've started playing IntelliJ idea ide

So from my understanding, a project in IntelliJ is the same as eclipse workspace Similarly, a module in IntelliJ is equivalent to a project in eclipse

I created a project in IntelliJ, but I still don't know why it should be a workspace SRC folder

After that, I created a module in the project I created a class in the SRC of the module The code I wrote in the classroom is as follows:

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 made an error while trying to read the file I'm trying to Txt file is placed in my SRC file, which is located inside the module, outside the SRC file, but in the module However, the file cannot be found in either case Yes, the code works, I try on eclipse, and it works normally The file name is also spelled correctly

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

If the image is too small, I'm sorry, you can find a larger version here: http://oi60.tinypic.com/jg1g60.jpg

Solution

Just move the file directly to the project folder named "Java" (so it's the home folder) (next to the blue blur you made: P)

Or if it doesn't help, test 123 Txt file to the firstjavaprogram directory

You can also change the file name to one of the following:

1)src / test123. txt

2)FirstJavaProgram / src / test123. txt

I don't know which one will be good in your case

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>