【Java IO】PrintStream打印流(属于字节输出流)的构造函数

打印流

  • PrintStream打印流,属于字节输出流
  • PrinterWriter,属于字符输出流

菜鸟教程上面的IO图

在这里插入图片描述

PrintStream的概念功能

  • PrintStream 为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式
  • 与其他输出流不同,PrintStream 永远不会抛出 IOException;
  • 异常情况仅设置可通过 checkError 方法测试的内部标志。
  • 为了自动刷新,可以创建一个 PrintStream;可以在写入byte 数组之后自动调用 flush 方法,可调用其中一个 println 方法,或写入一个换行符或字节 (’\n’)。
  • PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。在需要写入字符而不是写入字节的情况下,应该使用 PrintWriter 类。

PrintStream的构造函数

“自动flush”,就是每次执行print(), println(), write()函数,都会调用flush()函数;而“不自动flush”,则需要我们手动调用flush()接口。

  • PrintStream(OutputStream out)
    将“输出流out”作为PrintStream的输出流,不会自动flush,并且采用默认字符集

  • PrintStream(OutputStream out, boolean autoFlush)
    将“输出流out”作为PrintStream的输出流,自动flush,并且采用默认字符集。

  • PrintStream(OutputStream out, boolean autoFlush, String charsetName)
    将“输出流out”作为PrintStream的输出流,自动flush,采用charsetName字符集。

  • PrintStream(File file)
    创建file对应的FileOutputStream,然后将该FileOutputStream作为PrintStream的输出流,不自动flush,采用默认字符集。

  • PrintStream(File file, String charsetName)
    // 创建file对应的FileOutputStream,然后将该FileOutputStream作为PrintStream的输出流,不自动flush,采用charsetName字符集。

  • PrintStream(String fileName)
    创建fileName对应的FileOutputStream,然后将该FileOutputStream作为PrintStream的输出流,不自动flush,采用默认字符集。

  • PrintStream(String fileName, String charsetName)
    创建fileName对应的FileOutputStream,然后将该FileOutputStream作为PrintStream的输出流,不自动flush,采用charsetName字符集。

おすすめ

転載: blog.csdn.net/weixin_44121966/article/details/119415237