VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 721|回复: 4

一个包装类的问题

[复制链接]

1

主题

2

帖子

3.00

积分

新手上路

Rank: 1

积分
3.00
发表于 2020-2-8 03:00:01 | 显示全部楼层 |阅读模式
import java.io.*;
public class MyBufferedReader extends BufferedReader {
private Reader in;
private char cb[];
private int nChars, nextChar;
private static final int INVALIDATED = -2;
private static final int UNMARKED = -1;
private int markedChar = UNMARKED;
private int readAheadLimit = 0;
private boolean skipLF = false;
private boolean markedSkipLF = false;
private static int defaultCharBufferSize = 8192;
private static int defaultExpectedLineLength = 80;
private static int j=0;
public MyBufferedReader(Reader in, int sz) {
super(in);
if (sz <= 0)
throw new IllegalArgumentException("Buffer size <= 0");
this.in = in;
cb = new char[sz];
nextChar = nChars = 0;
}
public MyBufferedReader(Reader in) {
this(in, defaultCharBufferSize);
}
private void ensureOpen() throws IOException {
if (in == null)
throw new IOException("Stream closed");
}
private void fill() throws IOException {
int dst;
if (markedChar <= UNMARKED) {
dst = 0;
} else {
int delta = nextChar - markedChar;
if (delta >= readAheadLimit) {
markedChar = INVALIDATED;
readAheadLimit = 0;
dst = 0;
} else {
if (readAheadLimit <= cb.length) {
System.arraycopy(cb, markedChar, cb, 0, delta);
markedChar = 0;
dst = delta;
} else {
char ncb[] = new char[readAheadLimit];
System.arraycopy(cb, markedChar, ncb, 0, delta);
cb = ncb;
markedChar = 0;
dst = delta;
}
nextChar = nChars = delta;
}
}
int n;
do {
n = in.read(cb, dst, cb.length - dst);
} while (n == 0);
if (n > 0) {
nChars = dst + n;
nextChar = dst;
}
}
String readLine(boolean ignoreLF) throws IOException { //覆盖了父类的readLine()方法
StringBuffer s = null;
int startChar;
boolean omitLF = ignoreLF || skipLF;
synchronized (lock) {
ensureOpen();
bufferLoop:
for (;;) {
if (nextChar >= nChars)
fill();
if (nextChar >= nChars) {
if (s != null && s.length() > 0)
return s.toString();
else
return null;
}
boolean eol = false;
char c = 0;
int i;
if (omitLF && (cb[nextChar] == '\n'))
nextChar++;
skipLF = false;
omitLF = false;
charLoop:
for (i = nextChar; i < nChars; i++) {
c = cb[i];
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;
}
j++;
return str=j+":"+str;
}
if (s == null)
s = new StringBuffer(defaultExpectedLineLength);
s.append(cb, startChar, i - startChar);
}
}
}
public String readLine() throws IOException {
return readLine(false);
}
public boolean ready() throws IOException {
synchronized (lock) {
ensureOpen();
if (skipLF) {
if (nextChar >= nChars && in.ready()) {
fill();
}
if (nextChar < nChars) {
if (cb[nextChar] == '\n')
nextChar++;
skipLF = false;
}
}
return (nextChar < nChars) || in.ready();
}
}
}

class Test7
{
public static void main(String args[]) throws Exception
{
File f1=new File("test7.java");
MyBufferedReader in=new MyBufferedReader(new FileReader(f1));
while(in.ready())
{
System.out.println(in.readLine());
}
}
}
这里挺热闹的有那位 IT大虾 能帮小弟解决这个问题呀!问题是怎么最后一行怎么打印不出行号
回复

使用道具 举报

0

主题

3

帖子

4.00

积分

新手上路

Rank: 1

积分
4.00
发表于 2020-5-25 08:45:01 | 显示全部楼层
最后一行没有\r \n,所以检查一下你的eol
回复

使用道具 举报

0

主题

2

帖子

3.00

积分

新手上路

Rank: 1

积分
3.00
发表于 2020-5-29 22:15:01 | 显示全部楼层
if ((c == '\n') || (c == '\r')) {
eol = true;
这个地方有问题,还要加上是否是最后一行的判断,因为最后一行写完后没有回车换行。
回复

使用道具 举报

0

主题

2

帖子

3.00

积分

新手上路

Rank: 1

积分
3.00
发表于 2020-5-30 23:45:01 | 显示全部楼层
if (s == null)
s = new StringBuffer(defaultExpectedLineLength);
s.append(cb, startChar, i - startChar);
return j+":"+s;
就可以了!
回复

使用道具 举报

1

主题

2

帖子

3.00

积分

新手上路

Rank: 1

积分
3.00
 楼主| 发表于 2020-7-13 16:30:02 | 显示全部楼层
谢谢nsre434654
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

快速回复 返回顶部 返回列表