'I can't get netbeans to find a txt file I have in the same directory... java.io.FileNotFoundException

I can't make it path specific because once I get this program to work (this is the last thing I have to do) I'm uploading to my university's ilearn website and it has to run on my professors computer with no modifications. I've tried a few different amalgamations of code similar to the following...

File file = new File("DataFile.txt");
Scanner document = new Scanner(new File("DataFile.txt"));

Or...

java.io.File file = new java.io.File("DataFile.txt");
Scanner document = new Scanner(file);

But nothing seems to work. I've got the necessary stuff imported. I've tried moving DataFile around in a few different folders (the src folder, and other random folders in the project's NetBeansProjects folder) I tried creating a folder in the project and putting the file in that folder and trying to use some kind of

documents/DataFile.txt

bit I found online (I named the folder documents).

I've tried renaming the file, saving it in different ways. I'm all out of ideas.

The file is just a list of numbers that are used in generating random data for this program we got assigned for building a gas station simulator. The program runs great when I just use user input from the console. But I can not get netbeans to find that file for the life of me! Help!?!?!?



Solution 1:[1]

Try adding the file to build path ..

Solution 2:[2]

public void readTextFile (){
try{
 Scanner scFile =new Scanner(new File("filename.txt");
while(scFile.hasNext()){
String line =scFile.nextLine();
Scanner details=new Scanner(line).useDelimiter("symbol");

than you can work from there to store integer values use e.g in an array
litterArr(size)=details.nextInt();
Note: size is a variable counting the size/number of info the array has.

}
scFile.close();
{
catch
(FILENOTFOUNDEXCEPION e){
..... *code*
}

Keep file in the same folder as the program,but if it is saved in another folder you need to supply the path indicating the location of the file as part of the file name e.g memAthletics.Lines.LoadFromFile('C:\MyFiles\Athletics.txt');

hope this helps clear the problem up :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Akhil Thayyil
Solution 2