I am a Java beginner and have read similar questions but still I dont get why my code is showing a FileNotFound Exception. My file is in the same directory.
My code is:
import java.io.*;
import java.util.Scanner;
public class reader {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
double y = in.nextDouble();
float g = in.nextFloat();
String a = in.next();
File file = new File("v.txt");
System.out.println(x + "" + y + "" + g + "" + a);
Scanner inFile = new Scanner(new FileReader(file));
String u = inFile.nextLine();
System.out.println(file.getAbsolutePath());
System.out.println(u);
}
}
Error is:
17: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
Scanner inFile = new Scanner(new FileReader(file));
^
1 error
exists()on file object before you perform read/write operations on it.FileNotFoundExceptionwill be thrown if the file couldn't be found during runtime. Your Error is triggered by the compiler, because your code is wrong (unhandled exception). This is something different and that is why I've asked you if you've searched for the error message.