import java.io.FileReader;
import java.io.IOException;
/**
* 自定义类模拟实现LineNumberReader类
* @author wjw
*
*/
public class Hello{
public static void main(String[] args)throws IOException {
MyBuffered m=new MyBuffered(new FileReader("g:\\java\\yes.txt"));
String str=null;
m.setCount(25);
while(null!=(str=m.myReadLine())){
System.out.println(m.getLineNumber()+":"+str);
}
}
}
class MyBuffered{
private FileReader fr;
private int count;
public MyBuffered(FileReader fr){
this.fr=fr;
}
public String myReadLine() throws IOException{
count++;//读取一行,行数+1;
int num=0;
StringBuilder sb=new StringBuilder();
while(-1!=(num=fr.read())){
if(num=='\r'){
continue;
}else if(num=='\n'){
return sb.toString();
}else{
sb.append((char)num);
}
}
if(sb.length()>0){
return sb.toString();
}
return null;
}
public int getLineNumber(){
return count;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
黑马程序员——自定义类模拟实现LineNumberReader类
最新推荐文章于 2023-11-30 18:55:56 发布