bufferedreader - Java Reader, read method for text file -
i have text file called test.txt word hello. trying read using reader.read() method , print contents console. when run getting number 104 printed on console , nothing else (i same number printed if change text more/less characters). ideas why behaving way , how can modify existing code print contents of test.txt string on console? here code:
public static void spellcheck(reader r) throws ioexception{ reader newreader = r; system.out.println(newreader.read()); }
and main method using test above:
public static void main (string[] args)throws ioexception{ reader test = new bufferedreader(new filereader("test.txt")); spellcheck(test); }
as the javadoc indicates, read() method reads single char, , returns int (in order able return -1 indicate end of stream). print int char, cast it:
int c = reader.read(); if (c != -1) { system.out.println((char) c); } else { system.out.println("end of stream"); }
to read everything, loop until -1, or read line line until null.
Comments
Post a Comment