ashimashi ashimashi - 2 months ago 34
Java Question

Reading files with Intellij idea IDE

I am a long time eclipse user and I have started to play around with IntelliJ Idea IDE.

So from my understanding. A project in IntelliJ is the same as the Eclipse Workspace. As well, a module in IntelliJ is the equivalent of a project in eclipse.

I created a project in IntelliJ, still not sure why there is a src folder in it if it is supposed to be a workspace...

Afterwards, I create a Module in the project. I created a class inside the src of the module. The code I made inside the class is the following:

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 file 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:

Picture

I apologize if the image is too small, you can find a larger version here: http://oi60.tinypic.com/jg1g60.jpg

Answer

just move the file directly to the project folder which calls "Java" (so the main folder) (next to the blue-blurred strip you made :P)

or if it doesnt 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

Comments