I wanted to read from a file using the Scanner class and when i implemented it it keeps throwing File-not-found exception but i checked multiple times with a couple of methods listed in the code and even made another file to read from and from different parts of the directory and still the same error was thrown but just as a put a try-catch block around the declaration of the Scanner object the program compiled and run without any errors
`import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
public class App extends EmployeeManagementSystem {
public static void main(String[] args) {
File inputFile = new File("/home/rayan/Documents/Assignment2/input2.txt");
File outputFile = new File("output.txt");
System.out.println(inputFile.exists());
System.out.println(outputFile.exists());
System.out.println(inputFile.canRead());
System.out.println(inputFile.getAbsolutePath());
Scanner sc = new Scanner(inputFile);
}
}
enter image description here
`
i tried placing the file in different locations and checking whether it was readable and also tried using the absolute path. I was expecting it to compile and the Scanner object to work
Rayan AG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.