码迷,mamicode.com
首页 > 其他好文 > 详细

IO异常处理

时间:2017-10-17 19:01:17      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:异常处理   port   编译   ati   style   exception   line   integer   color   

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异常处理

标签:异常处理   port   编译   ati   style   exception   line   integer   color   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!