Java fileinputstream No such file or directory -
i'm trying learn how use read line line text file. though put txt file in same src, console shows error - no such file or directory.
public class ddd { public static void main(string[] args) { fileinputstream fis = null; bufferedreader reader = null; try { fis = new fileinputstream("/dd/src/com/dd/input.txt"); reader = new bufferedreader(new inputstreamreader(fis)); system.out .println("reading file line line using bufferedreader"); string line = reader.readline(); while (line != null) { system.out.println(line); line = reader.readline(); } } catch (exception e) { system.out.println(e.getmessage()); } { try { reader.close(); fis.close(); } catch (ioexception e) { system.out.println(e.getmessage()); } } } }
the problem here:
"/dd/src/com/dd/input.txt" on linux @ least, absolute path. want instead path relative project root:
"src/com/dd/input.txt" or, if you're packaging file program, use resource stream instead of raw fileinputstream:
inputstream = ddd.class.getresourceasstream("input.txt");
Comments
Post a Comment