IO异常处理

import java.io.*;
public class StandardIO {
    public static void main(String[] args) {
        try {/*先使用System.in构造InputStreamReader,再构造BufferedReader*/
            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter a string:");
            System.out.println(stdin.readLine());
            System.out.print("Enter an integer:");
            int number1 = Integer.parseInt(stdin.readLine());/*将字符串解析为带符号的十进制整数*/
            System.out.println(number1);
            System.out.print("Enter a double:");
            double number2 = Double.parseDouble(stdin.readLine());
            System.out.println(number2);
        } catch(IOException e){System.err.println("IOException");}
    }
}
//Char_Ascii.java
public class Char_Ascii {
    public static void main(String[] args) {
        int ascii_value;
        char char_value = '0';
        for(int i = 1;i<=10;i++)
        {
            char_value = (char)System.in.read();
            ascii_value = (int)char_value;
            System.out.println(char_value + "<== =>" + ascii_value);
        }
    }
}

在Char_Ascii.java中编译会出现未捕获的IO异常错误

java中System.out和System.err 已被封装成PrintStream对象,因此具有强大的输出的功能,但System.in却仍然是原始的InutStream,需要在使用的时候进行封装

原文地址:https://www.cnblogs.com/gride-glory/p/7682670.html