黑马程序员-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);
            }
        }
    }

}

 

posted @ 2013-03-23 14:15  谷文仁  阅读(134)  评论(0编辑  收藏  举报