/*
这里面的append方法应该只是StringBuilder的方法,在毕老师的视频里看到的,就是提示有错
*/
import java.io.*;
class LineNumber1{
private Reader r;
private int lineNumber;
LineNumber1(Reader r){
this.r=r;
}
public String myReadLine(){
lineNumber++;
StringBuilder sb=new StringBuilder();
int ch=0;
while((ch=r.read())!=-1){
if(ch=='\r')
continue;
if(ch=='\n')
return sb.toString();
else
return sb.append((char)ch);
}
if(sb.length()!=0)
return sb.toString();
return null;
}
public void setLineNumber(int lineNumber){
this.lineNumber=lineNumber;
}
public int getLineNumber(int lineNumber){
return lineNumber;
}
public void myClose() throws IOException{
r.close();
}
}
public class LineNumber {
public static void main(String[] args) throws IOException {
FileReader fr=new FileReader("E:\\工作区\\eclipse\\JavaByHand\\src\\LineNumber.java");
LineNumber1 ln=new LineNumber1(fr);
String line=null;
while((line=ln.myReadLine())!=null){
System.out.println(line);
}
ln.myClose();
}
}
|