i use method in opening files, when opened project won't run because mac device. store txt file , should write instead of
(new file("d:\\description.txt"));
the method
scanner instream = null; try { instream = new scanner(new file("d:\\description.txt")); } catch (filenotfoundexception e) { system.out.println("erorr openenig file"); } while (instream.hasnextline ()) { string line = instream.nextline(); system.out.println(line); }
a couple of approaches can use individually, or combine:
hard-coding elements should left configurable. making path configurable, means can have different depending on platform on.
if file belongs with distribution, make sure stored @ class path, , access using yourclass.class.getresourceasstream("/description.txt")
; yourclass class in distribution. resource path relative location of class (yourclass), if want @ root of class path, need prefix forward slash "/". here, not need worry os conventions (forward vs backward slash). remarked else, should not consider file writable in case.
another typical approach, storing things configuration, specific 1 user, store @ default path location get's automatically resolved. example java system property "user.home". in case of windows environment, resolve %home% environment variable (something /user/myuserid
).