java - Custom readLine -
i need implement custom readline method, following signature:
public string readline(int maxlinelength) so i've created:
public string readline(int maxlinelength) throws readinglineexception { if (this.eofreached){ return null; } stringbuilder bld = new stringbuilder(); try { int readcount = 0; int charcode; while ((charcode = this.readable.read()) > -1 || readcount <= maxlinelength) { readcount += 1; bld.append((char)charcode); } if (readcount > maxlinelength){ throw new readinglineexception("maximum line length reached."); } if (charcode == -1) { this.eofreached = true; } return bld.tostring(); } catch (ioexception e) { e.printstacktrace(); throw new readinglineexception(); } } so.. how can detect end of line using read? don't think simple
(char)charcode == '\n' because not work in other system (where eol marked \n\r)
i though read additional char after \n , determine if \r, -1 or other char. if '\r' i've reached eol , ok, when -1 same case when other char, i'd start on next readline. proper way?
you ignore '\r'. if char '\r', read another.
Comments
Post a Comment