Why is the following code throwing a No Such File Found
Exception? The path is correct and the file does really exist.
Code:
java.util.Scanner s = new java.util.Scanner(
new File(getClass().getResource("file.txt").getFile()));
Why is the following code throwing a No Such File Found
Exception? The path is correct and the file does really exist.
Code:
java.util.Scanner s = new java.util.Scanner(
new File(getClass().getResource("file.txt").getFile()));
there is one ) missing at the end of your statement. the correct statement should be as follows:- Scanner s = new Scanner(new File(getClass().getResource("file.txt").getFile()));
If this was not what you were looking for, then probably this is the error scenario that you might be facing. I am assuming that you are writing this block of code in your main() method. As main is marked as static method and you are making use of the getClass() which is a non-static method, you should be getting an error saying "Cannot make a static reference to the non-static method getClass() from the type Object".
also, where exactly have you put your text file?? is the location present in your class path?? if not you will have to include it or you will have to give the complete path of the file in your getResource method.
Probably you can give more insights on the question you posted??