1. 程式人生 > >IO異常處理

IO異常處理

功能 and 捕獲 int try blog ber buffered asc

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,需要在使用的時候進行封裝

IO異常處理