黑马程序员-JAVA高级视频_IO输入与输出-19天-4(MyBufferedReader)
package itcast.java16; import java.io.FileReader; import java.io.IOException; /* * * 自定义readLine(); */ class MyBufferedReader { private FileReader fr; public MyBufferedReader(FileReader fr) { this.fr = fr; } public String myReadLine() throws IOException { StringBuilder sb = new StringBuilder(); int len = 0; while ((len = fr.read()) != -1) { if (len == '\r') continue; if (len == '\n') return sb.toString(); sb.append((char) len); } if (sb.length() != 0) { return sb.toString(); } return null; } public void myClose() throws IOException { if (fr != null) { fr.close(); } } } public class MyBufferedReaderDemo { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("demo.txt"); MyBufferedReader mr = new MyBufferedReader(fr); String len = null; while ((len = mr.myReadLine()) != null) { System.out.println(len); } } }