java - Why is my program catching / throwing a FileNotFoundException when the file exists? -
java newbie here!
i'm writing program practice reading input , writing output files. i've finished coding program, when run it, program catches , proceeds filenotfoundexception.
the file in source folder program, , i've tried placing in every folder related program. i've tried:
- declaring exceptions in method header
- surrounding section-in-question try/catch block.
- both of above together.
here's relevant code causing problems. there sticks out i'm missing?
public static void main(string[] args) throws filenotfoundexception { scanner keyboard = new scanner(system.in); string playerhighestscore = "", playerlowestscore = ""; int numplayers = 0, scorehighest = 0, scorelowest = 0; system.out.println("enter input file name: "); string inputfilename = keyboard.nextline(); string outputfilename = getoutputfilename(keyboard, inputfilename); file inputfile = new file(inputfilename); try { scanner reader = new scanner(inputfile); reader.close(); } catch (filenotfoundexception exception) { system.out.println("there problem reading file."); system.exit(0); } scanner reader = new scanner(inputfile); printwriter writer = new printwriter(outputfilename);
the answer simple. if filenotfoundexception
, reason file not found in given path.
if use ide, path working directory different source directory.
example, if using netbeans, source files inside /src
. working directory (.
) project directory.
in other hand, problem may thing @don mentioned. if going cross platform approach, can use "/
" in paths. works irrespective os.
example : string filename = "c:/directory/file.txt";
, these paths case sensitive. make sure use correct case. (it won't problem in windows, until package program.)
Comments
Post a Comment