- package com.examp.ch18;
- import java.io.*;
- class MyBufferedReader {
- private FileReader reader;
-
- private char[] str;
-
-
- public MyBufferedReader(FileReader reader){
- this.reader=reader;
- this.str=new char[1024];
- }
-
- public MyBufferedReader(Reader Filereader,int size){
- this.reader=reader;
- this.str=new char[size];
- }
-
-
- public String myReadLine() throws IOException{
- int ch=0;
- int num=0;
- while((ch=this.reader.read()) != -1){
-
- if(ch=='\r')
- continue;
- else if(ch=='\n'){
- return new String(str,0,num);
- }
- else{
- str[num]=(char)ch;
- num++;
- }
- }
- return new String(str,0,num);
- }
-
-
- public void myClose() throws IOException{
- this.reader.close();
- }
- }
- public class MyBufferedReaderDemo {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- MyBufferedReader mbr=null;
-
- String fileName="E:/testmybuf.txt";
-
- try{
- mbr=new MyBufferedReader(new FileReader(fileName));
-
- String readLine;
- while((readLine=mbr.myReadLine())!=null){
- System.out.println(readLine);
- }
- System.out.println("读取完毕!");
-
- }catch(IOException e){
- System.out.println("读取文件错误"+e.toString());
- }finally{
- try{
- if(mbr!=null){
- mbr.myClose();
- }
- }catch(IOException e){
- System.out.println("文件关闭时发生错误!"+e.toString());
- }
- }
- }
- }
复制代码
|
|