Java 标准输入输出流(System.in,System.out)使用

标准输入输出流

概述

System.in:标准的输入流,默认从键盘输入

System.out:标准的输出流,默认从控制台输出

可以使用System类的setIn(InputStream is)改变其输入对象

可以使用System类的setOut(PrintStream ps)改变其输出对象


使用

从键盘输入字符,之后在控制台输出其大写字符,输入e或exit时退出输入

public static void main(String[] args) {
    //先创建节点流对象(System.in是字节流)
    InputStream in = System.in;
    //通过转换流使其转换为字节流
    InputStreamReader isr = new InputStreamReader(in);
    //得到字节流通过缓冲流处理,提升传输速度
    BufferedReader reader = new BufferedReader(isr);

    //读取+处理操作
    while (true){
        String s = null;
        try {
            s = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if ("e".equals(s) || "exit".equals(s)){
            System.out.println("结束");
            break;
        }
        System.out.print(s.toUpperCase());
    }

    //关闭资源
    try {
        if (reader != null)
       		reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

版权声明:本文为m0_51517626原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。