如何打印 Reader 中的每一行

How can I print each line from a Reader

所以我正在做一个 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");
        }
}

我还要引用 Elliot Harold 的 "Java Network Programming 4th edition",因为作者说我们不应该使用 BufferedReader 或 DataInputStream 的 readLine() 方法,因为不同的操作系统有不同的换行符(\ n 或 \r\n 或其他)

此外,我没有使用这些流中的任何一个,所以即使我想使用 readLine() 也无法使用。

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

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

不,不是。它读取一个字符。

使用BufferedReader.readLine().

I am also going to quote "Java Network Programming 4th edition" by Elliot Harold because the author says that we shouldn't use the readLine() method of a BufferedReader or a DataInputStream because different operating systems have different newline delimeters (\n or \r\n or something else).

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