[Java IO]04_系统标准IO

目录

 

System表示系统类,它有3个与 Java IO 有关的常量。

  • System.out——系统标准输出
  • System.in——系统标准输入
  • System.err——错误信息输出  

 

 

System.out

 

System.out 是 PrintStream 的对象,在 PrintStream 中定义了一些了的 print() 和 println() 方法。

所以,常见的 System.out.print() 或 System.out.println() 语句调用的实际上是 PrintStream 类的方法。

例 使用 OutputStream 向屏幕上输出

复制代码
import java.io.OutputStream;
import java.io.IOException;

public class SystemDemo01 {
    public static void main(String args[]) {
        OutputStream out = System.out; // 此时的输出流是向屏幕上输出
        try {
            out.write("hello world!!!".getBytes()); // 向屏幕上输出
        } catch (IOException e) {
            e.printStackTrace(); // 打印异常
        }
        try {
            out.close(); // 关闭输出流
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
};
复制代码

 

System.in

 

System.in 是 InputStream 类型的对象,可以利用它完成从键盘读取数据的功能。

例 从键盘读取数据

复制代码
import java.io.InputStream;

public class SystemInDemo {
    public static void main(String args[]) throws Exception {
        InputStream input = System.in;
        
        byte b[] = new byte[5]; // 开辟空间,接收数据
        System.out.print("请输入内容:");
        
        int len = input.read(b); // 接收数据
        System.out.println("输入的内容为:" + new String(b, 0, len));
        
        input.close(); // 关闭输入流
    }
};
复制代码

运行结果

请输入内容:Good Bye
输入的内容为:Good

上述结果中,输入内容并没有被完整打印出来。

这是因为代码中限制了存储数据的 byte 数组的大小。

如果不知道要输入的数据的实际长度,如何处理呢?

例 不指定大小,从键盘读取数据

复制代码
import java.io.InputStream;

public class SystemInDemo2 {
    public static void main(String args[]) throws Exception { // 所有异常抛出
        InputStream input = System.in; // 从键盘接收数据

        StringBuffer buf = new StringBuffer(); // 使用StringBuffer接收数据
        System.out.print("请输入内容:");

        int temp = 0;
        while ((temp = input.read()) != -1) { // 接收内容
            char c = (char) temp;
            if (c == '\n') { // 退出循环,输入回车表示输入完成
                break;
            }
            buf.append(c); // 保存内容
        }
        System.out.println("输入的内容为:" + buf);

        input.close(); // 关闭输入流
    }
};
复制代码

运行结果

请输入内容:To be or not to be, that's a question.
输入的内容为:To be or not to be, that's a question.

但是,如果输入的是中文,则会出现乱码,如下:

请输入内容:计算机软件
输入的内容为:?????ú?í??

这是因为汉字一个字符占两个字节,而数据却一个一个字节的方式读进来的,所以造成了乱码。

最好的输入方式是将全部输入数据暂时存放在一块内存中,然后一次性从内存中读取出数据。

这样,既不会造成乱码,也不受长度限制。要完成这样的操作可以使用 BufferedReader 类去完成。

 

System.err

 

System.err 表示的是错误信息输出,如果程序出现错误,则可以直接使用 System.err 进行输出。

例 打印错误信息

复制代码
public class SystemErrDemo {
    public static void main(String args[]) {
        String str = null;
        try {
            System.out.println(Integer.parseInt(str)); // 转型
        } catch (Exception e) {
            System.err.println(e);
        }
    }
};
复制代码

运行结果

java.lang.NumberFormatException: null

 

IO 重定向

System 类可以改变 System.in 的输入流来源以及 System.out 和 System.err 两个输出流的输出位置。

复制代码
import java.io.*;

public class RedirectDemo {
    public static void main(String[] args) throws IOException {
        PrintStream console = System.out;
        BufferedInputStream in = new BufferedInputStream(new FileInputStream("d:\\in.txt")); // 绑定输入文件
        PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("d:\\out.txt"))); // 绑定输出文件

        // 设置重定向
        System.setIn(in);
        System.setOut(out);
        System.setErr(out);

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null) {
            System.out.println(s);
            out.close();
            System.setOut(console);
        }
    }
}
复制代码

需要注意的是,I/O重定向操纵的是字节流,而不是字符流。

 

参考资料

Java 编程思想

Java 开发实战经典

posted on 2020-01-16 14:36  星云留水  阅读(175)  评论(0编辑  收藏  举报

导航