Reputation: 21
I need to know how to prompt user for a file and then be able to use the file in my program. Here is my unsuccessful attempt.
public static void main(String[] args) {
Scanner Scanscan = new Scanner(System.in);
System.out.print("Input filename:");
String filename = Scanscan.nextLine();
File inputFile = new File(filename);
Scanner reader = new Scanner(inputFile);
I get a "File not found exception" when I try it.
Upvotes: 1
Views: 19431
Reputation: 31
File does not exist. You should,
1.Check that your file is existed corresponding to your executing application.
2.Try to provide full hard path of file in your system like "C:\\sample.txt"
Upvotes: 2
Reputation: 7920
You get a FileNotFoundException
because the filename the user entered doesn't exist in the system. Enter a filename that does exist, and it should work. Make sure the file you're getting is in the directory you're running the program from. Try entering the name of the java file you're working on amd see if it works.
Upvotes: 4