Java的三种简单的控制台输入和输出方式

时间:2025-02-15 18:17:59
    //控制台输出
    public static void ScannerInputAndOut(){
        Scanner in = new Scanner();
        (());
    }
    /**
     * 字节流
     */
    public static void ByteInputAndOut(){
        //输入
        BufferedInputStream in = new BufferedInputStream();
        try {
            byte[] b = new byte[1024];
            (b);
            (new String(b));
        } catch (IOException e) {
            ();
        }
        //输出
        BufferedOutputStream out = new BufferedOutputStream();
        try {
            for (int i = 0; i < 5; i++) {
                ("hello".getBytes());
            }
            ();
        } catch (IOException e) {
            ();
        }
    }

    /**
     * 字符流
     */
    public static void CharInputAndOut(){
        //输入
        BufferedReader in = new BufferedReader(new InputStreamReader());
        try {
            (());
        } catch (IOException e) {
            ();
        }
        //输出
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter());
        try {
            for (int i = 0; i < 5; i++) {
                ("hello"+i);
                ();
            }
            ();
        } catch (IOException e) {
            ();
        }
    }