text - Confusion with J2me reading file. Please Help me understand -
i working on j2me app symbian s60 phones reading text file required. have no access bufferedreader extract line of text file, did find in nokia forums, , has me bit confused. here's code, , question below. answering.
/** * reads single line using specified reader. * @throws java.io.ioexception if exception occurs when reading * line */ private string readline(inputstreamreader reader) throws ioexception { // test whether end of file has been reached. if so, return null. int readchar = reader.read(); if (readchar == -1) { return null; } stringbuffer string = new stringbuffer(""); // read until end of file or new line while (readchar != -1 && readchar != '\n') { // append read character string. operating systems // such microsoft windows prepend newline character ('\n') // carriage return ('\r'). part of newline character // , therefore exception should not appended // string. string.append((char)readchar); // read next character readchar = reader.read(); } return string.tostring(); }
my question regarding readline() method. in while() loop, why must check readchar != -1 , != '\n' ? understanding -1 represents end of stream (eof). understanding if extracting 1 line, should have check newline char.
thanks.
please read code documentation carefully. doubts answered in that.
the function checking ‘-1’ because taking care of streams comes without new line character. in case return whole stream string.
Comments
Post a Comment