I'm trying to read from a file using the Scanner and File class:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class TextFileReaderV1
{
public static void main(String[] args) throws IOException
{
String token = "";
File fileName = new File("data1.txt");
Scanner inFile = new Scanner(fileName);
while( inFile.hasNext() )
{
token = inFile.next( );
System.out.println(token);
}
inFile.close();
}
}
However, it is saying, "no such file or directory". and giving me the "java.io.FileNotFoundException"
I am using IntelliJ IDEA and the file is in the current directory I am working in: src/data1.txt -> next to GetFile.java (current code)
Full Error Message:
Exception in thread "main" java.io.FileNotFoundException: data1.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at GetFile.main(GetFile.java:19)
**Edit: ** It has been solved!! The run configuration was set to the project directory, not the src one, so I implicitly added it in the argument:
File fileName = new File("src/data1.txt");
The run configuration was set to the project directory, not the src one, so I implicitly added it in the argument:
File fileName = new File("src/data1.txt");