标准的输入输出流 了解

本文讲解了Java中标准输入输出流的使用,包括如何通过System.in从键盘接收输入,并演示了将输入转换为大写并持续交互,直到用户输入'exit'退出程序。还介绍了MyInput.java中的方法用于不同数据类型从键盘读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


标准的输入输出流 了解


package com.atguigu.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * 其他流的使用
 * 1. 标准的输入、输出流
 * 2. 打印流
 * 3. 数据流
 *
 * @author liangqichen
 * @create 2021-10-17 22:27
 */
public class OtherStream {

    /*
    1. 标准的输入、输出流
        1.1
    System.in  标准的输入流,默认从键盘输入
    System.out 标准的输出路,默认从控制台输出
        1.2
    System类的setIn(InputStream)  setOut(PrintStream) 方式重新指定输入和输出的流。
        1.3练习
    从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续讲进行输入操作,值至当输入 e 或者 exit时,退出程序

    方法一 : 使用Scanner,调用了next方法,返回一个字符串
    方法二 : 使用System.in实现  System.in --- > 转换流--- >BufferedReader的readline()
     */
    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            InputStreamReader isr = new InputStreamReader(System.in);
            br = new BufferedReader(isr);
            while (true) {
                System.out.println("输入数据");
                String data = br.readLine();
                if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
                    System.out.println("程序结束");
                    break;
                }
                String upperCase = data.toUpperCase();
                System.out.println(upperCase);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }}
    

package com.atguigu.exer;
// MyInput.java: Contain the methods for reading int, double, float, boolean, short, byte and
// string values from the keyboard

import java.io.*;

public class MyInput {
    // Read a string from the keyboard
    public static String readString() {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        // Declare and initialize the string
        String string = "";

        // Get the string from the keyboard
        try {
            string = br.readLine();

        } catch (IOException ex) {
            System.out.println(ex);
        }

        // Return the string obtained from the keyboard
        return string;
    }

    // Read an int value from the keyboard
    public static int readInt() {
        return Integer.parseInt(readString());
    }

    // Read a double value from the keyboard
    public static double readDouble() {
        return Double.parseDouble(readString());
    }

    // Read a byte value from the keyboard
    public static double readByte() {
        return Byte.parseByte(readString());
    }

    // Read a short value from the keyboard
    public static double readShort() {
        return Short.parseShort(readString());
    }

    // Read a long value from the keyboard
    public static double readLong() {
        return Long.parseLong(readString());
    }

    // Read a float value from the keyboard
    public static double readFloat() {
        return Float.parseFloat(readString());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值