- package com.itcast.day4;
- import java.io.FileReader;
- import java.io.IOException;
- public class MyBufferedReaderDemo {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
-
- FileReader fr = new FileReader("ListDemo.java");
- MyBufferedReader myBufr = new MyBufferedReader(fr);
- String line = null;
- while((line=myBufr.myReadLine())!=null) {
- System.out.println(line);
- }
- myBufr.myClose();
- }
- }
- class MyBufferedReader {
-
- private FileReader fr;
- public MyBufferedReader(FileReader fr) {
- this.fr = fr;
- }
- public String myReadLine() throws IOException{
- 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();
- }
- }
复制代码 |
|