如何从Reader打印每一行

时间:2017-04-06 00:11:40

标签: java inputstream writer

所以我正在开发一个我正在制作SMTP服务器的项目。我试图读取每行输入,然后将该行交给另一个线程进行处理。但是,使用此代码,在连接结束之前不会发送这些行。我认为这与新行不会打破while循环这一事实有关:

while (true) {
        StringBuilder line = new StringBuilder();
        try {
            int c;
            while ((c = in.read()) != -1){ //this reads the line
                line.append((char) c); //add the character to our line
            }
            String response = Parser.parse(line.toString); //give the line we just read to the parser for processing
        } catch (IOException ex) {
            System.err.println("IOException while reading the clients mesasge");
        }
}

我还要引用#34; Java网络编程第4版"作者:Elliot Harold,因为作者说我们不应该使用BufferedReader或DataInputStream的readLine()方法,因为不同的操作系统有不同的换行符(\ n或\ r \ n或其他)

另外,我没有使用其中任何一个流,所以即使我想使用readLine(),我也无法做到。

任何人都知道我如何才能完成我想要做的事情

1 个答案:

答案 0 :(得分:1)

while ((c = in.read()) != -1){ //this reads the line

不,它没有。它读取一个字符。

使用BufferedReader.readLine()

  

我还要引用#34; Java网络编程第4版"作者:Elliot Harold因为作者说我们不应该使用BufferedReader或DataInputStream的readLine()方法,因为不同的操作系统有不同的换行符(\ n或\ r \ n或其他)。

Elliot Rusty Harold需要阅读Javadoc,你也是。这是A级BS。请参阅亚马逊的评论,了解本书中的一长串错误。

相关问题