package IO;
import java.io.*;
class MybufferedReader1 {
/**
* @param args
*/
private static FileReader r;
MybufferedReader1(FileReader r) {
this.r = r;
}
public String myreadline()throws Exception {
int ch = 0;
StringBuilder sb = new StringBuilder();
while ((ch = r.read()) != -1) {//读取文件,返回给ch,当读完等于-1,循环停止
if (ch=='\r') {
continue;//当读到\r表示换行,这个数据不进入内存
}
if (ch=='\n') {
return sb.toString();//当读到\n,就把这行的数据交给字符串
}else
sb.append((char) ch);
}
return null;}
public class MybufferedReader{
public static void main(String[] args) {
FileReader fr=null;
BufferedReader br=null;
try {
fr=new FileReader("c:\\demo.txt");
br=new BufferedReader(fr);
String str=null;
while(br.myreadline()!=null){}
} catch (Exception e) {
System.out.println("读取异常");
}
}
为什么显示红线呢 |