java - FileNotFoundException while file exists in same folder as program -
similar questions deal files on c:
drive, hardcoding file path acceptable answer. application mobile, , hardcoding file path not practical.
i trying import text file via scanner, contains list of strings, 15 characters each, 1 per line. lots of lines. file name a.txt
.
i phone call using
file info = new file("a.txt"); scanner in = new scanner(data);
repeated lines below "b.txt", using different object names.
however, when build program, error log (using jcreator ide):
--------------------configuration: <default>-------------------- e:\simple encryption\simple_encryption.java:17: unreported exception java.io.filenotfoundexception; must caught or declared thrown scanner in = new scanner(data); ^ e:\simple encryption\simple_encryption.java:18: unreported exception java.io.filenotfoundexception; must caught or declared thrown scanner numsin = new scanner(nums); ^ 2 errors process completed.
the programme in folder e:/simple encryption/
, contains next files , folders:
a.txt
on import, 1 line below) /images (folder, contains icon.png) there nil else in folder. cannot hardcode file path because on different computers, has different drive names, , distributed under different folders later. (here (school), it's on e:
drive, @ home it's j:
drive, , @ work it's on g:
drive.)
although have gotten help existing errors, need import on programme startup, , not skip it. file exists in it's current form, , programme cannot function without it.
what causing error? , can prevent this?
e:\simple encryption\simple_encryption.java:17: unreported exception java.io.filenotfoundexception; must caught or declared thrown
as exception saying need utilize try- grab block
try { scanner numsin = new scanner(nums); grab (filenotfoundexception e) { // } { if (numsin != null) numsin .close(); }
or
you need throw exception though calling method
public static void xxxx() throws filenotfoundexception { // }
java java.util.scanner filenotfoundexception
No comments:
Post a Comment