黑马程序员JAVA高级视频_IO输入与输出19天9(读取键盘录入)

package string.itcastio;

import java.io.*;

/*
 * 当录入一行数据后,就将该行数据经行打印
 * 如果录入的数据是over,那么停止录入
 */
public class IODemo1 {
    public static void main(String[] args) throws IOException {
        InputStream is = System.in;
        StringBuilder sb = new StringBuilder();
        while (true) {
            int ch = is.read();
            if (ch == '\r')
                continue;
            if (ch == '\n') {
                String s = sb.toString();
                if ("over".equals(s)) {
                    break;
                }
                System.out.println(s);
                sb.delete(0, sb.length());

            } else {
                sb.append((char)ch);
            }
        }
    }

}
原文地址:https://www.cnblogs.com/guwenren/p/2977125.html