Java Java类
这个用于写入字符流的抽象类。子类必须实现的唯一方法是 write(char[], int, int)、flush() 和 close()。然而,大多数子类将覆盖此处定义的一些方法,以提供更高的效率、附加功能或两者兼而有之。
构造函数
- protected Writer() :创建一个新的字符流编写器,其关键部分将在编写器本身上同步。
- protected Writer(Object lock) :创建一个新的字符流编写器,其关键部分将在给定对象上同步。
方法:
- Writer append(char c) :将指定的字符附加到这个 writer。调用 out.append(c) 形式的这个方法的行为与调用完全相同
out.write(c)Syntax :public Writer append(char c) throws IOException Parameters: c - The 16-bit character to append Returns: This writer Throws: IOException
- Writer append(CharSequence csq) :将指定的字符序列附加到这个 writer。调用 out.append(csq) 形式的这个方法的行为与调用完全相同
out.write(csq.toString())
根据字符序列 csq 的 toString 规范,可能不会附加整个序列。例如,调用字符缓冲区的 toString 方法将返回一个子序列,其内容取决于缓冲区的位置和限制。Syntax :public Writer append(CharSequence csq) throws IOException Parameters: csq - The character sequence to append. If csq is null, then the four characters "null" are appended to this writer. Returns: This writer Throws: IOException
- Writer append(CharSequence csq, int start, int end) :将指定字符字符的子序列追加到此 writer
Syntax :public Writer append(CharSequence csq, int start, int end) throws IOException Parameters: csq - The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null". start - The index of the first character in the subsequence end - The index of the character following the last character in the subsequence Returns: This writer Throws: IndexOutOfBoundsException IOException
- abstract void close() :关闭流,首先刷新它。一旦流被关闭,进一步的 write() 或 flush() 调用将导致抛出 IOException。关闭以前关闭的流没有效果。
Syntax :public abstract void close() throws IOException Throws: IOException
- abstract void flush() :刷新流。如果流已将来自各种 write() 方法的任何字符保存在缓冲区中,则立即将它们写入其预期目的地。然后,如果该目的地是另一个字符或字节流,则刷新它。因此,一次 flush() 调用将刷新 Writers 和 OutputStreams 链中的所有缓冲区。
Syntax :public abstract void flush() throws IOException Throws: IOException
- void write(char[] cbuf) :写入一个字符数组。
Syntax :public void write(char[] cbuf) throws IOException Parameters: cbuf - Array of characters to be written Throws: IOException - If an I/O error occurs
- abstract void write(char[] cbuf, int off, int len) :写入字符数组的一部分。
Syntax :public abstract void write(char[] cbuf, int off, int len) throws IOException Parameters: cbuf - Array of characters off - Offset from which to start writing characters len - Number of characters to write Throws: IOException
- void write(int c) :写入单个字符。要写入的字符包含在给定整数值的低 16 位中; 16 个高位被忽略。
打算支持高效单字符输出的子类应覆盖此方法。Syntax :public void write(int c) throws IOException Parameters: c - int specifying a character to be written Throws: IOException
- void write(String str) :写入一个字符串。
Syntax :public void write(String str) throws IOException Parameters: str - String to be written Throws: IOException
- void write(String str, int off, int len) :写入字符串的一部分。
Syntax :public void write(String str, int off, int len) throws IOException Parameters: str - A String off - Offset from which to start writing characters len - Number of characters to write Throws: IndexOutOfBoundsException
程序 :
//Java program demonstrating Writer methods
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
class WriterDemo
{
public static void main(String[] args) throws IOException
{
Writer wr=new PrintWriter(System.out);
char c[] = {'B','C','D','E','F'};
CharSequence cs = "JKL";
String str = "GHI";
//illustrating write(int c)
wr.write(65);
//flushing the stream
wr.flush();
//illustrating write(char[] c,int off,int len)
wr.write(c);
wr.flush();
//illustrating write(String str,int off,int len)
wr.write(str);
wr.flush();
//illustrating append(Charsequence cs,int start,int end)
wr.append(cs);
wr.flush();
//illustrating append(int ch)
wr.append('M');
wr.flush();
//closing the stream
wr.close();
}
}
输出 :
ABCDEFGHIJKLM