本帖最后由 haixian 于 2014-3-28 17:11 编辑
String readLine(boolean ignoreLF) throws IOException {
StringBuffer s = null;
int startChar;
synchronized (lock) {
ensureOpen();//判断输入流是否为空
boolean omitLF = ignoreLF || skipLF;
bufferLoop:
for (;;) {
if (nextChar >= nChars)
fill();//当前缓冲已经读完,填充缓冲
if (nextChar >= nChars) { /* EOF *///读到类文件末尾
if (s != null && s.length() > 0)
return s.toString();
else
return null;
}
boolean eol = false;
char c = 0;
int i;
/* Skip a leftover '\n', if necessary */
if (omitLF && (cb[nextChar] == '\n'))
nextChar++;
skipLF = false;
omitLF = false;
charLoop:
for (i = nextChar; i < nChars; i++) {//从缓冲中每次取一行
c = cb;
if ((c == '\n') || (c == '\r')) {
eol = true;
break charLoop;//读到类一行的末尾
}
}
startChar = nextChar;
nextChar = i;
if (eol) {
String str;
if (s == null) {
str = new String(cb, startChar, i - startChar);
} else {
s.append(cb, startChar, i - startChar);
str = s.toString();
}//生成返回的字符串
nextChar++;
if (c == '\r') {
skipLF = true;
}
return str;
}
if (s == null)
s = new StringBuffer(defaultExpectedLineLength);
s.append(cb, startChar, i - startChar);
}
}
}
我把我知道的写上去了,他就是先把数据读到一个缓冲数组,然后每次从数组中取一行出来。。
|