- class MyReader {
- public Reader reader;
- public int index = 1; // 行号
- public MyReader(Reader reader) {
- this.reader = reader;
- }
- public String readLine() throws IOException { //将异常交给方法调用处处理
- StringBuilder s = new StringBuilder(index + " : ");
- int buf;
- while ((buf = reader.read()) != 10 && buf != -1) { //当读到'\n'时停止读取
- s.append((char) buf);
- }
- index++;
- if (buf == -1)
- return null;
- else
- return s.toString();
- }
- public void close() throws IOException { //提供close方法,关闭资源;
- if (reader != null) {
- reader.close();
- }
- }
- }
复制代码 |