关于键盘录入的小Demo

import java.io.IOException;
import java.io.InputStream;

public class ReadKey3 {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        readKey3();

    }

    /*
     * 
     * 获取用户键盘录入的数据, 并将数据变成大写显示在控制台上, 如果用户输入的是over,结束键盘录入。
     */
    public static void readKey3() throws IOException {
        // 1,创建容器。
        StringBuilder sb = new StringBuilder();

        // 2,获取键盘读取流。
        InputStream in = System.in;

        // 3,定义变量记录读取到的字节,并循环获取。
        int ch = 0;

        while ((ch = in.read()) != -1) {

            // 在存储之前需要判断是否是换行标记 ,因为换行标记不存储。
            if (ch == '
')
                continue;
            if (ch == '
') {
                String temp = sb.toString();
                if ("over".equals(temp))
                    break;
                System.out.println(temp.toUpperCase());
                sb.delete(0, sb.length());
            } else
                // 将读取到的字节存储到StringBuilder中。
                sb.append((char) ch);

            
        }

    }
}
原文地址:https://www.cnblogs.com/stonewu/p/3674142.html