zxy_cola
Nullii
2016-03-27 17:13

IO流 自定义readLine 无法读取下一行

5
  • io流

import java.io.*;
public class Demo
{
public static void main(String[] args) throws IOException
{
BufferedWriter bfw = new BufferedWriter(new FileWriter("b.txt"));
bfw.write("sdasdaad\r\n");

    bfw.write("1");
    bfw.close();

    myreadline bfr = new myreadline(new FileReader("b.txt"));
    String s = null;
    while((s = bfr.method()) != null)
    {
        sop(s);
    }
    bfr.myclose();

}

public static void sop(Object obj)
{
    System.out.println(obj);
}

}

class myreadline extends Reader
{
private FileReader r ;
myreadline(FileReader r)
{
this.r = r;
}

public String method() throws IOException
{
    StringBuilder sb = new StringBuilder();

    int ch =0;
    while((ch = r.read()) != -1)
    {
        if( ch == '\r')
            continue;
        if(ch == '\n')
            return sb.toString();
        else
            sb.append((char)ch);
    }
    return null;
}

public void myclose()throws IOException
{
    r.close();
}

public void close()throws IOException
{
    r.close();
}

public int read(char[] c , int a , int b) throws IOException
{
    return r.read(c, a , b);
}

}

为什么只能读取一行呢???

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换