20230625 java.io.PrintWriter

发布时间 2023-08-23 10:01:11作者: 流星<。)#)))≦

介绍

  • java.io.PrintWriter
  • public class PrintWriter extends Writer
  • 对应的字节流是 PrintStream
  • 主要作用是文本输出
  • 可以作为装饰器包装 WriterOutputStream ,会装饰成 BufferedWriter

API

构造器

  • PrintWriter (Writer out)
  • PrintWriter(Writer out, boolean autoFlush)
  • PrintWriter(OutputStream out)
  • PrintWriter(OutputStream out, boolean autoFlush)
  • PrintWriter(OutputStream out, boolean autoFlush, Charset charset)
  • PrintWriter(String fileName) throws FileNotFoundException
  • PrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException
  • PrintWriter(String fileName, Charset charset) throws IOException
  • PrintWriter(File file) throws FileNotFoundException
  • PrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException
  • PrintWriter(File file, Charset charset) throws IOException
    • csn 表示 charsetName ,字符集名称
    • autoFlush 表示 自动冲刷模式(默认禁用),只要 println 被调用,缓冲区中的所有字符被 flush
      • autoFlush 只影响 println 方法,不影响其他方法

public

  • checkError
    • 如果产生格式化或输出错误,则返回 true
    • 一旦这个流碰到了错误,它就受到了污染,并且所有对 checkError 的调用都将返回 true
  • print
    • 通过打印从 toString 产生的字符串来打印一个对象
  • println
    • 打印一个字符串,后面紧跟一个行终止符。如果这个流处于自动冲刷模式,那么就会冲刷这个流
  • printf
    • 按照格式字符串指定的方式打印给定的值
  • format
    • 使用指定的格式字符串和参数将格式化字符串写入此 Writer 。如果启用自动刷新,则调用此方法将刷新输出缓冲区