java.lang.System对IO的支持

 

public final class System extends Object;
1.public static final PrintStream out        // 全局常量,标准输出流;
    The “standard” output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.
2.public static final PrintStream err        // 全局常量,标准错误输出流;
    The “standard” error output stream. This stream is already open and ready to accept output data.
    Typically this stream corresponds to display output or another output destination specified by the host environment or user. By convention, this output stream is used to display error messages or other information that should come to the immediate attention of a user even if the principal output stream, the value of the variable out, has been redirected to a file or other destination that is typically not continuously monitored.
3.public static final InputStream in        // 全局常量,标准输入流
    The “standard” input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user.  
–以上常量都是流类;每个类都实现了底层;
—-使用标准输出流:
package com.mldn;
import .io.*;
public class OutDemo
{
        public static void main(String[] args)
        {
                OutputStream out = System.out;        // 实例化输出流对象;
                try
                {
                        out.write(“Hello, world!\n”.getBytes());        // 把byte[]的数据写入out流,out流将定位到标准输出;
                }
                catch (IOException e)
                {
                        e.printStackTrace();
                }
                try
                {
                        out.close();        // 关闭流输出
                }
                catch (IOException e)
                {
                        e.printStackTrace();
                }               
        }
}
/*
administrator@xu-desktop:~$ com.mldn.OutDemo
Hello, world!
标准输出流关联显示器,也是OutputStream的子类,属于打印流;
*/
———–使用标准错误输出流:
package com.mldn;
public class ErrorDemo
{
        public static void main(String[] args)
        {
                String str = “Hello, world!”;        // 非数字字符串;
                try
                {
                        System.out.println(Integer.parseInt(str));        // 明显的非法转换
                }
                catch (NumberFormatException e)        // 捕获一个运行时
                {
                        System.err.println(“非法的类型转换: \n” + e);                // 使用标准错误输出流打印
                }
        }
}
/*
administrator@xu-desktop:~$ com.mldn.ErrorDemo
非法的类型转换:
.lang.NumberFormatException: For input string: “Hello, world!”
~“““标准错误流属于打印输出流;
*/

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《java.lang.System对IO的支持
本文地址:https://www.zhiletu.com/archives-93.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微