一个关于java按行读取的函数如下public String readUntil(StringBuffer sb,InputStream in) {
try {
     String line;
     BufferedReader reader = new BufferedReader(new InputStreamReader(in));
     line = reader.readLine();      // 读取第一行 
     while (line!=null) {           // 如果 line 为空说明读完了 
        sb.append(line);            // 将读到的内容添加到 buffer 中 
      sb.append("\n");            // 添加换行符 
      line = reader.readLine();   // 读取下一行  **
     }
     System.err.println(sb.toString());
     return sb.toString();
}catch (Exception e) {e.printStackTrace();}
return null;
}不能正常返回读取的字符串
调试时执行到(**)标记的这一行就不执行了
有哪位高手能帮忙解决下?
不胜感激

解决方案 »

  1.   

    改成while(!line.equals(""))。试试
      

  2.   

    改成这样试试   
     BufferedReader br = new BufferedReader(new InputStreamReader(in));
           while ((thisLine = br.readLine()) != null) {
                 sb.append(line);           
                 sb.append("\n"); 
           }
      

  3.   

    一楼的会抱空指针异常的
    二楼的方法就是改了一下我的方法的写法而已
    没做实际的更改方法执行一段时间后就会打印如下信息
    Welcome to Microsoft Telnet Service 
    login: 
    Session timed out.Telnet Server has closed the connection但是我希望在打印如下信息后就返回该字符串
    Welcome to Microsoft Telnet Service 
    login: 
    而不是等到session超时后返回
    高手指点
      

  4.   

    line = reader.readLine();  
    检查下 line的返回值!
    明确问题所在,代码没有问题,有可能文件条件不满足或外部传入的参数问题等
      

  5.   

    执行到line = reader.readLine();这一行以后line为""
    但调试到这一行就不执行了
    这个不知道是什么原因
      

  6.   

    这样试试!public String readUntil(StringBuffer sb,InputStream in) {
        try {
             String line;
             BufferedReader reader = new BufferedReader(new InputStreamReader(in));
             int c;
             do
             {
                 String line= reader.readLine();
                 sb.append(line);           
                   sb.append("\n");            
             }while ((c=reader.read())!=-1);
        }catch (Exception e) {e.printStackTrace();}
        return null;
    }
      

  7.   

    关键是对方并没有发回车字符回来,但是同时也未结束流
    readLine是会一直阻塞直到流中返回了回车字符,又或者流结束了
      

  8.   

    呵呵,对于流操作不是太熟悉,也很少用,不过看上去似乎是读取到最后的时候,应该不会是null,就是你调试的"",这个时候,你就判断一下.有的说没有问题,有点说有问题,就处理一下输入的格式.改输入格式,然后调试,或者改程序,调试,慢慢来,肯定行的.
      

  9.   

    package ggg;import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    import java.io.InputStreamReader;public class Test
    {
    public static void main(String args[])
    { InputStream in;
    try {
    in = new FileInputStream("\\t.txt");
    StringBuffer sb=new StringBuffer();
    new Test().readUntil(sb, in);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

    }
    public String readUntil(StringBuffer sb,InputStream in) {
        try {
             String line;
             BufferedReader reader = new BufferedReader(new InputStreamReader(in));
             line = reader.readLine();      // 读取第一行 
             while (line!=null) {           // 如果 line 为空说明读完了 
                 sb.append(line);            // 将读到的内容添加到 buffer 中 
                 sb.append("\n");            // 添加换行符 
                 line = reader.readLine();   // 读取下一行  **
             }
             System.out.println(sb.toString());
             return sb.toString();
        }catch (Exception e) {e.printStackTrace();}
        
        return null;
    }
    }
    //这个程序正确,楼主你怎么了?
      

  10.   

    在eclipse 安装的盘符下随便建一个t.txt就知道你输得程序没有问题呀!
      

  11.   


    public String readUntil(StringBuffer sb,InputStream in) { try { String line; BufferedReader reader = new BufferedReader(new InputStreamReader(in)); ; // 读取第一行  while ((line = reader.readLine())!=null) { // 如果 line 为空说明读完了  sb.append(line); // 将读到的内容添加到 buffer 中  sb.append("\n"); // 添加换行符  line = reader.readLine(); // 读取下一行 **  } System.err.println(sb.toString()); return sb.toString(); }catch (Exception e) {e.printStackTrace();} return null; }
    这样试试
      

  12.   

    line = reader.readLine();      // 读取第一行 
             while (line!=null) {           // 如果 line 为空说明读完了 
                sb.append(line);            // 将读到的内容添加到 buffer 中 
                 sb.append("\n");            // 添加换行符 
                line=null;//是不是这里要添加这行?             
    line = reader.readLine();   // 读取下一行  **
             }
     while (line!=null) 这样判断行么? 与null比较吗?
      

  13.   

    package readline;import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;public class ReadLine { /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    String string = br.readLine();
    if(string ==null){
    System.out.println(string+"是空值!");
    }
    if("".equals(string)){
    System.out.println(string+"是‘’");
    }
    System.out.println(string);
    }}
    我测试过了是你循环的条件不对。
      while ("".equals(line)) {           // 如果 line 为空说明读完了 
      

  14.   

    循环条件有问题,在条件对比时由于无法判断对错形成死循环,使方法停在 line = reader.readLine();这一行,而不继续执行。在循环外加上这句:int len = 1;在循环里加上这句:len = iine.length();循环条件改为:(len != 0),应该会ok的。
      

  15.   

    不好意思,循环里那句打错了个字母,应该是:len = line.length();
      

  16.   

    可以试试BufferedReader里的skip(long)方法,跳过去
      

  17.   

    不行,Socket获得的流是会阻塞的。应该是到了那里没有独到换行符,回车符导致堵塞
      

  18.   

    吧一个字节流对象 保存到session里面 楼主想要做什么哦
      

  19.   

    J2EE技术交流群 67488968
    相互交流,共同提高,期待你的加入
      

  20.   

    结果是出来了
    不过存在如下乱码
    如何才能过滤掉这些乱码?
    望高手指教
    Welcome to Microsoft Telnet Service login: administrator
    password: *===============================================================                Welcome to Microsoft Telnet Server.                                             *===============================================================                C:\Documents and Settings\Administrator>                                        hostnamemwgj-wmwC:\Documents and Settings\Administrator>
      

  21.   

    telnet是不可以直接使用InputStream的,因为里面有控制字符,你需要将控制字符跳过
      

  22.   

    感觉除了没有reader.close(); ,没有问题
      

  23.   

    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String line = null
    while ((line = br.readLine()) != null) {
                 sb.append(line);           
    }
      

  24.   

    楼主用我这个吧,很稳定,效率比用行来读好,而且支持不同编码.

          /** getStrFromIS
    * get string from an InputStream object
    * @param is: a nInputStream object 
    * @param charSet if charSet is null or "", use the system default charSet 
    * @Return a String of the InputStream. 
           */
    public static String getStrFromIS(InputStream is,String charSet) throws Exception{
    ByteArrayOutputStream os = new ByteArrayOutputStream(); 
    int read;
    byte[] bytes = new byte[BUFSIZE]; //BUFSIZE 是一个常量10240=10k
    while ((read = is.read(bytes)) != -1) {
    os.write(bytes, 0, read);
    }
    is.close();

    String str;
    if (charSet==null || charSet.length() == 0) 
    str = os.toString();
    else
    str = os.toString(charSet); 

    return str;

      

  25.   


    *===============================================================                
    Welcome to Microsoft Telnet Server.                                             
    *===============================================================                
    C:\Documents and Settings\Administrator>                                        dir
    驱动器 C 中的卷没有标签。
    卷的序列号是 BCFD-343D
    C:\Documents and Settings\Administrator 的目录
    2008-12-21  17:16    <DIR>          .
    2008-12-21  17:16    <DIR>          ..
    2008-12-21  12:58    <DIR>          .datastudio
    2008-12-21  13:13    <DIR>          .myeclipse
    2008-12-21  13:14               124 .myeclipse.properties
    2008-12-20  13:36    <DIR>          Bluetooth Software
    2008-12-21  13:09    <DIR>          Contacts
    2008-12-20  19:36    <DIR>          Favorites
    2008-12-21  13:10    <DIR>          My Documents
    2008-12-21  17:10    <DIR>          「开始」菜单
    2009-02-12  11:27    <DIR>          桌面
    1 个文件            124 字节
    10 个目录  2,905,075,712 可用字节
    C:\Documents and Settings\Administrator>2009-02-12  11:27    <DIR>          桌面                          
                   1 个文件            124 字节
                  10 个目录  2,905,075,712 可用字节                        
    C:\Documents and Settings\Administrator>dir                                     
    驱动器 C 中的卷没有标签。  
    卷的序列号是 BCFD-343D
    C:\Documents and Settings\Administrator 的目录
     
    7:16    <DIR>          ..         
    2:58    <DIR>          .datastudio
    3    <DIR>          .myeclipse           
    1  13:14               124 .myeclipse.properties
    0  13:36    <DIR>          Bluetooth Software
    1  13:09    <DIR>          Contacts 
    0  19:36    <DIR>          Favorites   
    3:10    <DIR>          My Documents                                
    8-12-21  17:10    <DIR>          「开始」菜单
    2009-02-12  11:27    <DIR>          桌面                                        
     1 个文��           124 字节                                     
    10 个目录  2,905,075,712 可用字节
    C:\Documents and Settings\Administrator>郁闷
    执行第一次的时候没问题
    第二次就出现问题了
      

  26.   

    我也遇到了类似的问题,是在读取inputstream的时候发生的
    我每次读取一定长度的流,在如下情况就会堵死
    流的长度是8,我每次读4,第二次读完后读取的长度是4,然后读第三次,正常的情况我认为是返回值为-1,这样就不继续读了,可是事实是第三次就堵死了