Nullii 2016-03-27 17:13 采纳率: 33.3%
浏览 1754
已结题

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

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条回答

  • threenewbee 2016-03-27 22:12
    关注

    StringBuilder sb = new StringBuilder();
    这个sb放在局部变量里,每次调用都不会再有之前读取的缓冲的字符了。

    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元