This looks like a test project at this stage but you ought to think longer term about using getResourceAsStream to load a resource.
At the moment, you are relying on the working directory of the JVM to locate that file using a relative path. If you were to open a terminal, change to the project's 'bin' directory and try running the program, you'd probably find it doesn't work. If you package it into a JAR file, it definitely won't work.
Using getResourceAsStream is the portable and reliable solution (and no more difficult really):
Code:
package example;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class Driver {
public static void main(String[] args) throws IOException {
InputStream in = Driver.class.getResourceAsStream("/data/mcdata.txt");
if (in != null) {
DataInputStream dataStream = new DataInputStream(in);
System.out.println((dataStream.read()));
}
else {
System.err.println("Resource not found");
}
}
}
The directory structure of the project looks like this (bear in mind, the Driver class is in the example package):
Code:
src:
data example
src/data:
mcdata.txt
src/example:
Driver.java
Bookmarks