I am using Eclipse to compile and run my java codes.
Here is Error I am getting.
Exception in thread \"main\" java.io.FileNotFoundEx
Either follow @rohit Jains approach or give the absolute path for your file like:
Scanner KB = new Scanner(new File("C:/JsfProjects/Project/file1.txt"));
while (KB.hasNext()) {
String line = KB.nextLine();
System.out.println(line);
}
Try passing the complete path to the file, say:
new File("/usr/home/mogli/file.txt")
Or if you're in windows:
new File("C:/Users/mogli/docs/file.txt")
In Windows try giving real path like this
"C:\\Users\\mogli\\docs\\file.txt"
It worked for me.
Your file should directly be under the project folder, and not inside any other sub-folder.
So, if your project folder is MyProject
, it's folder structure(not complete though) should be like: -
MyProject +- src +
| |
| +-- Your source file
+- file.txt
It should not be under src
folder.
Or, you can give the following path relative to the project folder to search for file in the src folder
: -
new File("src/file.txt");