Okay, you have two choices...
Option #01...
You can include the file inside the project itself, by placing the file inside the src
directory. This will embed the file in your program, making it readonly

(Yes, I know I'm using Netbeans, the concept is the same in Eclipse)
As you can see the studentData.txt
is in the myawesomeproject
, along side my Main
class. Where in your src
you place the does matter, so beware of that, if the file is NOT in the same package, then you will need to provide a relative or fully qualified path to it.
Next, we use Scanner input = new Scanner(getClass().getResourceAsStream("studentData.txt"))
to open the file, for example...
package myawesomeproject;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
try {
loadStudent();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
public void loadStudent() throws FileNotFoundException {
try (Scanner input = new Scanner(getClass().getResourceAsStream("studentData.txt"))) {
while (input.hasNext()) {
String info = input.nextLine();
System.out.println(info);
String elements[] = info.split(" , ");
String fName = elements[0];
String lName = elements[1];
String phone = elements[2];
String address = elements[3];
double gpa = Double.parseDouble(elements[4]);
String major = elements[5];
}
}
}
}
Which in my example, prints...
B1 , B2 , B3 , B4 , 0 , B6
(which is the contents of my file)
Option #02...
You place the file in the "working" directory, this is the context from which the program is executed, typically, this is the project directory (the directory which contains the src
directory)

Then you can use Scanner input = new Scanner(new File("studentData.txt"))
to open it, for example...
package myawesomeproject;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
try {
loadStudent();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
public void loadStudent() throws FileNotFoundException {
try (Scanner input = new Scanner(new File("studentData.txt"))) {
while (input.hasNext()) {
String info = input.nextLine();
System.out.println(info);
String elements[] = info.split(" , ");
String fName = elements[0];
String lName = elements[1];
String phone = elements[2];
String address = elements[3];
double gpa = Double.parseDouble(elements[4]);
String major = elements[5];
}
}
}
}
Which outputs the same thing as above, since I used the same file and just moved it for the example
This does assume you've not changed the "working" directory. If you can test where the working directory is by using System.out.println(System.getProperty("user.dir"));
, you would then move your file to this location
System.out.println(new File(".").getCanonicalPath());
orSystem.out.println(System.getProperty("user.dir"));
to your code, this will tell where your program is been executed from, the file should reside within that directory