所有类


java.io
类 Writer

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

public abstract class Writer
   
   
   
   
   
extends Object
implements Appendable, Closeable, Flushable

写入字符流的抽象类。子类必须实现的方法仅有 write(char[], int, int)、flush() 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。

从以下版本开始:
JDK1.1
另请参见:
Writer, BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, FileWriter, PipedWriter, PrintWriter, StringWriter, Reader

字段摘要
protected  Objectlock
          用于同步针对此流的操作的对象。
 
构造方法摘要
protected Writer()
          创建一个新的字符流 writer,其关键部分将同步 writer 自身。
protected Writer(Object lock)
          创建一个新的字符流 writer,其关键部分将同步给定的对象。
 
方法摘要
 Writerappend(char c)
          将指定字符追加到此 writer。
 Writerappend(CharSequence csq)
          将指定字符序列追加到此 writer。
 Writerappend(CharSequence csq, int start, int end)
          将指定字符序列的子序列追加到此 writer.Appendable
abstract  voidclose()
          关闭此流,但要先刷新它。
abstract  voidflush()
          刷新此流。
 voidwrite(char[] cbuf)
          写入字符数组。
abstract  voidwrite(char[] cbuf, int off, int len)
          写入字符数组的某一部分。
 voidwrite(int c)
          写入单个字符。
 voidwrite(String str)
          写入字符串。
 voidwrite(String str, int off, int len)
          写入字符串的某一部分。
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

lock

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

构造方法详细信息

Writer

protected Writer()
创建一个新的字符流 writer,其关键部分将同步 writer 自身。


Writer

protected Writer(Object lock)
创建一个新的字符流 writer,其关键部分将同步给定的对象。

参数:
lock - 要同步的对象。
方法详细信息

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 - 要写入的字符数
抛出:
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
写入字符串的某一部分。

参数:
str - 字符串
off - 相对初始写入字符的偏移量
len - 要写入的字符数
抛出:
IOException - 如果发生 I/O 错误

append

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

out.append(csq) 形式的方法调用与以下调用具有完全相同的行为:

     out.write(csq.toString()) 

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

指定者:
接口 Appendable 中的 append
参数:
csq - 要追加的字符串序列。如果 csqnull,则向此 writer 追加四个字符 "null"
返回:
此 writer
抛出:
IOException - 如果发生 I/O 错误
从以下版本开始:
1.5