刚才代码没粘上
- import java.io.*;
- class Test12
- {
- public static void main(String[] args) throws IOException
- {
- MyBufferedReader mbr=new MyBufferedReader(new FileReader("D:\\Test12.java"));
- String line=null;
- while((line=mbr.myReadLine())!=null){
- System.out.println(mbr.getLineNumber()+":"+line);
-
- }
- mbr.close();
- }
- }
- class MyBufferedReader
- {
- private Reader r;
- int line=0;
- MyBufferedReader(Reader r){
- this.r=r;
- }
- public String myReadLine()throws IOException{
- line++;
- StringBuilder sb=new StringBuilder();
- int len=0;
- while((len=r.read())!=-1){
- if(len=='\r')
- continue;
- else if(len=='\n')
- return sb.toString();
- else
- sb.append(((char)len));
- }
- if(sb!=null)
- return sb.toString();
- return null;
- }
- public int getLineNumber(){
- return line;
- }
- public void setLineNumber(int line){
- this.line=line;
- }
- public void close()throws IOException{
- r.close();
- }
- }
复制代码 |