import java.io.*;
//重新定义readLine()函数
class MyBufferedReader{
private FileReader fr;//定义FileReader引用
MyBufferedReader(FileReader fr){
this.fr=fr;
}
public String MyReadLine()throws IOException{//自定义MyReadLine()函数
StringBuilder sb=new StringBuilder();
int num=0;
while((num=fr.read())!=-1){//调用FileReader的read()函数逐个读取数据
if(num=='\r')//回车符回到上面代码继续
continue;
if(num=='\n')//换行符返回数据
return sb.toString();
sb.append((char)num);//循环添加数据
}
if(sb.length()!=0)
return sb.toString();
return null;
}
public void MyClose()throws IOException{
fr.close();
}
}
class MyBufReader {
public static void main(String[] args) {
MyBufferedReader mybuf=null;
try {
FileReader fr=new FileReader("demo.txt");
mybuf=new MyBufferedReader(fr);
while(mybuf.MyReadLine()!=null){
System.out.println(mybuf.MyReadLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
mybuf.MyClose();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
文件内容:
rdasas
dasds
sda
dssda
输出内容则是:
dasds
dssda |
|