Module java.base
Package java.io

Class Writer

java.lang.Object
java.io.Writer
所有已实现的接口:
Closeable, Flushable, Appendable, AutoCloseable
直接已知的子类:
BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter

public abstract class Writer extends Object implements Appendable, Closeable, Flushable
用于向字符流写入的抽象类。子类必须实现的唯一方法是write(char[], int, int),flush()和close()。但是,大多数子类将覆盖此处定义的一些方法,以提供更高的效率、额外的功能或两者兼有。
自:
1.1
参见:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Object
    用于同步此流上操作的对象。
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Writer()
    创建一个新的字符流写入器,其关键部分将在写入器本身上同步。
    protected
    Writer(Object lock)
    创建一个新的字符流写入器,其关键部分将在给定对象上同步。
  • Method Summary

    Modifier and Type
    Method
    Description
    append(char c)
    将指定的字符追加到此写入器。
    将指定的字符序列追加到此写入器。
    append(CharSequence csq, int start, int end)
    将指定字符序列的子序列追加到此写入器。
    abstract void
    close()
    关闭流,首先刷新它。
    abstract void
    flush()
    刷新流。
    static Writer
    返回一个新的Writer,它会丢弃所有字符。
    void
    write(char[] cbuf)
    写入字符数组。
    abstract void
    write(char[] cbuf, int off, int len)
    写入字符数组的一部分。
    void
    write(int c)
    写入单个字符。
    void
    write(String str)
    写入字符串。
    void
    write(String str, int off, int len)
    写入字符串的一部分。

    Methods declared in class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • lock

      protected Object lock
      用于同步此流上操作的对象。为了提高效率,字符流对象可能使用除自身以外的对象来保护关键部分。因此,子类应该使用此字段中的对象,而不是this或同步方法。
  • Constructor Details

    • Writer

      protected Writer()
      创建一个新的字符流写入器,其关键部分将在写入器本身上同步。
    • Writer

      protected Writer(Object lock)
      创建一个新的字符流写入器,其关键部分将在给定对象上同步。
      参数:
      lock - 用于同步的对象
  • Method Details

    • nullWriter

      public static Writer nullWriter()
      返回一个新的Writer,它会丢弃所有字符。返回的流最初是打开的。通过调用close()方法关闭流。对close()的后续调用没有效果。

      在流打开时,append(char)append(CharSequence)append(CharSequence, int, int)flush()write(int)write(char[])write(char[], int, int)方法均不执行任何操作。流关闭后,这些方法都会抛出IOException

      用于同步返回的Writer上操作的对象未指定。

      返回:
      丢弃所有字符的Writer
      自:
      11
    • write

      public void write(int c) throws IOException
      写入单个字符。要写入的字符包含在给定整数值的低16位中;高16位将被忽略。

      打算支持高效单字符输出的子类应该重写此方法。

      参数:
      c - 指定要写入的字符的int值
      抛出:
      IOException - 如果发生I/O错误
    • write

      public void write(char[] cbuf) throws IOException
      写入字符数组。
      参数:
      cbuf - 要写入的字符数组
      抛出:
      IOException - 如果发生I/O错误
    • write

      public abstract void write(char[] cbuf, int off, int len) throws IOException
      写入字符数组的一部分。
      参数:
      cbuf - 字符数组
      off - 开始写入字符的偏移量
      len - 要写入的字符数
      抛出:
      IndexOutOfBoundsException - 如果实现应在off为负、len为负或off + len为负或大于给定数组的长度时抛出此异常
      IOException - 如果发生I/O错误
    • write

      public void write(String str) throws IOException
      写入字符串。
      参数:
      str - 要写入的字符串
      抛出:
      IOException - 如果发生I/O错误
    • write

      public void write(String str, int off, int len) throws IOException
      写入字符串的一部分。
      实现要求:
      此类中的实现对指定条件抛出IndexOutOfBoundsException;重写方法可以选择执行其他操作。
      参数:
      str - 一个字符串
      off - 开始写入字符的偏移量
      len - 要写入的字符数
      抛出:
      IndexOutOfBoundsException - 如果实现应在off为负、len为负或off + len为负或大于给定字符串的长度时抛出此异常
      IOException - 如果发生I/O错误
    • append

      public Writer append(CharSequence csq) throws IOException
      将指定的字符序列追加到此写入器。

      形式为out.append(csq)的此方法的调用行为与调用方式完全相同

          out.write(csq.toString())
      

      根据字符序列csqtoString规范,可能不会追加整个序列。例如,调用字符缓冲区的toString方法将返回一个子序列,其内容取决于缓冲区的位置和限制。

      指定者:
      append 在接口 Appendable
      参数:
      csq - 要追加的字符序列。如果csqnull,则将四个字符"null"追加到此写入器。
      返回:
      此写入器
      抛出:
      IOException - 如果发生I/O错误
      自:
      1.5
    • append

      public Writer append(CharSequence csq, int start, int end) throws IOException
      将指定字符序列的子序列追加到此写入器。Appendable

      csq不为null时,形式为out.append(csq, start, end)的此方法的调用行为与调用方式完全相同

          out.write(csq.subSequence(start, end).toString())
      
      指定者:
      append 在接口 Appendable
      参数:
      csq - 从中追加子序列的字符序列。如果csqnull,则将字符追加为如果csq包含四个字符"null"
      start - 子序列中第一个字符的索引
      end - 子序列中最后一个字符之后的字符的索引
      返回:
      此写入器
      抛出:
      IndexOutOfBoundsException - 如果startend为负,start大于end,或end大于csq.length()
      IOException - 如果发生I/O错误
      自:
      1.5
    • append

      public Writer append(char c) throws IOException
      将指定的字符追加到此写入器。

      out.append(c)形式调用此方法的行为与调用方式完全相同

          out.write(c)
      
      指定者:
      append 在接口 Appendable
      参数:
      c - 要追加的16位字符
      返回:
      此写入器
      抛出:
      IOException - 如果发生I/O错误
      自:
      1.5
    • flush

      public abstract void flush() throws IOException
      刷新流。如果流从各种write()方法保存了任何字符到缓冲区中,请立即将它们写入到它们的目标位置。然后,如果该目标是另一个字符流或字节流,请刷新它。因此,一个flush()调用将刷新所有写入器和输出流链中的所有缓冲区。

      如果此流的目标是底层操作系统提供的抽象,例如文件,则刷新流仅保证先前写入流的字节被传递给操作系统进行写入;它不保证它们实际被写入物理设备,如磁盘驱动器。

      指定者:
      flush 在接口 Flushable
      抛出:
      IOException - 如果发生I/O错误
    • close

      public abstract void close() throws IOException
      关闭流,首先刷新它。一旦流被关闭,进一步的write()或flush()调用将导致抛出IOException。关闭先前关闭的流没有任何效果。
      指定者:
      close 在接口 AutoCloseable
      指定者:
      close 在接口 Closeable
      抛出:
      IOException - 如果发生I/O错误