- import java.io.*;
- /*模拟readLine代码*/
- class MyBufferedReader
- {
- private FileReader fr;
- MyBufferedReader(FileReader fr)
- {
- this.fr=fr;
- }
- public String myReadLine() throws IOException
- {
- StringBuilder sb=new StringBuilder();
-
- int num=0;
- while((num=fr.read())!=-1)
- {
- if(num!='\r')
- continue;
- if(num=='\n')
- return sb.toString();
- else
- sb.append((char)num);
- }
- if(sb.length()!=0)
- return sb.toString();
- return null;
- }
- public void myClose() throws IOException
- {
- fr.close();
- }
- }
- class MyBufferedReaderDemo
- {
- public static void main(String[] args) throws IOException
- {
- FileReader fr=new FileReader("demo.txt");
- MyBufferedReader mr=new MyBufferedReader(fr);
- String line=null;
- while((line=mr.myReadLine())!=null)
- {
- System.out.println(line);
- }
- mr.myClose();
- }
- }
复制代码
|
|