Reputation: 3
I have been trying to read a file into Eclipse. I've looked over other questions, but those answers did not remedy the situation (refreshing the project folder, using getProperty and specifying the correct path, etc.) I've moved the file into every folder and I get the same error. I've copied the file into the directory as shown here:
I've also pasted the code below. It's stupidly simple. The error I get is "FileInputStream.open(String) line: not available [native method]".
Any help would be appreciated. Code is below.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Modulo {
public static void main(String[] args) throws FileNotFoundException {
File questions = new File("input.txt");
Scanner sc = new Scanner(questions);
while(sc.hasNext()){
int x = sc.nextInt();
String divide = sc.next();
int y = sc.nextInt();
System.out.println(x % y);
}
}
}
Upvotes: 0
Views: 114
Reputation: 36304
Eclipse by default looks for the file in the main project directory in your case Remainders
, if your file is not there, you get an exception. Try placing the file directly under your project and run the same program, it should run correctly.
Upvotes: 0
Reputation: 347334
The answer depends.
If you want the file to be embedded within your application when your deploy it (as a Jar file for example), then you can't use File
to reference it, as you've tried to include it within your application.
Eclipse further complicates the matter, as you can't included resources within your src
directory, but needs to be maintained within a resources
directory at the same level as your src
folder (this folder may need to be included as part of your build process, but I only have a passing knowledge of how Eclipse works)...
Once you've corrected for all this, you will then need to use Class#getResource
to load the resource...for example...
try (InputStream is = Modulo.class.getResourceAsStream("/input.txt")) {
Scanner sc = new Scanner(is);
//...
} catch (IOException exp) {
exp.printStackTrace();
}
However, if you want the file to be an external resource to your program, then you need to place it within a location relative to the location that the program is executed.
Normally, I would suggest the project directory, but I have a funny feeling that Eclipse run's it's Java programs in a different location ... and I don't know if you can change it...
In this case, you could use System.out.println(new File(".").getAbsolutePath());
or System.out.println(new File(".").getCanonicalPath());
or System.out.println(System.getProperty("user.dir"));
which will tell you where you program is currently running and place the file there.
Of course, once build (into a Jar) you would need to place the file within a context that was relative to the location it was executed from...
Upvotes: 2
Reputation: 2006
You copy that file into Project folder parallel to src. This is the place base path of your code.
Upvotes: 0