标签:ade out red while new err imp 方便 return
import java.io.*;
public class MyBufferedReader {
private FileReader fr;
MyBufferedReader(FileReader fr)
{
this.fr = fr;
}
//可以一次性读一行数据的方法
public String myReadLine() throws IOException
{
//定义一个临时容器。原BufferReader封装的是字符数组。
//为了演示方便,定义一个StringBuilder容器。因为最终还是要将数据变成字符串
StringBuilder sb = new StringBuilder();
int ch = 0;
while((ch = fr.read()) != -1)
{
if(ch == ‘\r‘)
continue;
if(ch == ‘\n‘)
return sb.toString();
else
sb.append((char)ch);
}
if(sb.length() != 0) //若有数据,则把剩下的数据返回
return sb.toString();
return null;
}
public void myClose() throws IOException
{
fr.close();
}
public static void main(String[] args) throws IOException {
MyBufferedReader mbr = new MyBufferedReader(new FileReader("buf_copy.txt"));
String str = null;
while((str = mbr.myReadLine()) != null)
{
System.out.println(str);
}
mbr.myClose();
}
}
标签:ade out red while new err imp 方便 return
原文地址:https://www.cnblogs.com/coolcpp/p/bufferedreader.html