Similar questions deal with files on the C: drive, where hardcoding the file path is an acceptable answer. This application is mobile, and hardcoding the file path is not practical.
I am trying to import a text file via scanner, that contains a list of strings, 15 characters each, 1 per line. LOTS of lines. The file name is a.txt.
I call it using
File data = new File("a.txt");
Scanner in = new Scanner(data);
repeated the lines below with “b.txt”, using different Object names.
However, when I build the program, I get this error log (using Jcreator IDE):
--------------------Configuration: <Default>--------------------
E:\Simple Encryption\Simple_Encryption.java:17: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
Scanner in = new Scanner(data);
^
E:\Simple Encryption\Simple_Encryption.java:18: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
Scanner numsIn = new Scanner(nums);
^
2 errors
Process completed.
The program is in the folder E:/Simple Encryption/, which contains the following files and folders:
- Simple_Encryption.java
- Simple_Encryption.class
- lock.png (unused)
- a.txt
- b.txt (same error as
a.txton import, just one line below) - /images (folder, contains icon.png)
There is nothing else in the folder. I cannot hardcode the file path because on different computers, it has different drive names, and will be distributed under different folders later. (Here (School), it’s on the E: drive, at home it’s the J:drive, and at work it’s on the G: drive.)
Although I have gotten help from existing errors, I need to import this on the program startup, and not just skip it. The file exists in it’s current form, and the program cannot function without it.
What is causing this error? And what can I do to prevent this?
As your exception saying you need to use try- catch block
OR
you need to throw that exception though calling method