Java readline() skipping second line -
i have questions file i'd read, , when reading, want identify questions answers , print them, before each questions there line of "#" characters, code keeps skipping question 1 reason? missing here?
here code:
try { // open file first // command line parameter fileinputstream fstream = new fileinputstream(path); bufferedreader br = new bufferedreader(new inputstreamreader(fstream)); string strline; strline = br.readline(); system.out.println(strline); // read file line line while ((strline ) != null) { strline = strline.trim(); if ((strline.length()!=0) && (strline.charat(0)=='#' && strline.charat(1)=='#')) { strline = br.readline(); system.out.println(strline); //questions[q] = strline; } strline = br.readline(); } // close input stream fstream.close(); // system.out.println(questions[0]); } catch (exception e) {// catch exception if system.err.println("error: " + e.getmessage()); }
i suspect, file read in utf-8 bom.
the bom code before first character, helps identify proper encoding of textfiles.
the issue bom is, invisible , disturbs reading. textfile bom arguable no longer textfile. especially, if read first line, first character no longer #, different, because character bom+#.
try load file explicit encoding specified. java can handle bom in newer releases, don't remember exactly.
bufferedreader br = new bufferedreader(new inputstreamreader(fstream, "utf-8")); otherwise, take decent text editor, notepad++ , change encoding utf-8 without bom or ansi encoding (yuck).

Comments
Post a Comment