IO流(读取键盘录入)

来源:互联网 发布:mysql front最新 编辑:程序博客网 时间:2024/04/29 18:43

读取键盘录入:

System.out:对应的是标准的输出设备,控制台。

System.in:对应的是标准的输入设备,键盘。


需求:

通过键盘录入数据。

当录入一行数据后,就将该行数据进行打印。

如果录入的数据是over,那么停止录入。

import java.io.*;class ReadIn{    public static void main(String[] args) throws IOException    {        InputStream in = System.in;        // int ch = 0;        StringBuilder sb= new StringBuilder();        while(true)        {            int ch = in.read();            if(ch=='\r')                continue;            if(ch=='\n')            {                String s = sb.toString();                if("over".equals(s))                    break;                System.out.println(s.toUpperCase());                sb.delete(0,sb.length());            }            else                sb.append((char)ch);        }    }}

0 0
原创粉丝点击