這個抽象類用於寫入字符流。子類必須實現的唯一方法是 write(char[], int, int)、flush() 和 close()。然而,大多數子類將重寫此處定義的一些方法,以提供更高的效率和/或附加函數。
構造函數
- 受保護Writer():創建一個新的字符流編寫器,其關鍵部分將在編寫器本身上同步。
- 受保護的寫入器(對象鎖):創建一個新的字符流編寫器,其關鍵部分將在給定對象上同步。
方法:
- 寫入器追加(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 返回: This writer Throws: IOException
- 寫入器追加(CharSequence csq):將指定的字符序列附加到此 writer。以 out.append(csq) 形式調用此方法的行為方式與調用完全相同
out.write(csq.toString())
根據字符序列 csq 的 toString 規範,可能不會附加整個序列。例如,調用字符緩衝區的 toString 方法將返回一個子序列,其內容取決於緩衝區的位置和限製。Syntax :public Writer append(CharSequence csq) throws IOException 參數: csq - The character sequence to append. If csq is null, then the four characters "null" are appended to this writer. 返回: This writer Throws: IOException
- 寫入器追加(CharSequence csq,int開始,int結束):將指定字符序列的子序列附加到此 writer。將指定字符序列的子序列附加到此 writer
Syntax :public Writer append(CharSequence csq, int start, int end) throws IOException 參數: 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
- 抽象無效close():關閉流,首先刷新它。一旦流關閉,進一步的write()或flush()調用將導致拋出IOException。關閉之前關閉的流沒有任何效果。
Syntax :public abstract void close() throws IOException Throws: IOException
- 抽象無效flush():刷新流。如果流已在緩衝區中保存了來自各種 write() 方法的任何字符,請立即將它們寫入其預期目的地。然後,如果該目的地是另一個字符或字節流,則刷新它。因此,一次 flush() 調用將刷新 Writers 和 OutputStreams 鏈中的所有緩衝區。
Syntax :public abstract void flush() throws IOException Throws: IOException
- 無效寫入(char[] cbuf):寫入字符數組。
Syntax :public void write(char[] cbuf) throws IOException 參數: cbuf - Array of characters to be written Throws: IOException - If an I/O error occurs
- 抽象無效寫入(char[] cbuf,int off,int len):寫入字符數組的一部分。
Syntax :public abstract void write(char[] cbuf, int off, int len) throws IOException 參數: cbuf - Array of characters off - Offset from which to start writing characters len - Number of characters to write Throws: IOException
- 無效寫入(int c):寫入單個字符。要寫入的字符包含在給定整數值的低 16 位中; 16 個高位被忽略。
想要支持高效單字符輸出的子類應該重寫此方法。Syntax :public void write(int c) throws IOException 參數: c - int specifying a character to be written Throws: IOException
- 無效寫入(字符串str):寫入一個字符串。
Syntax :public void write(String str) throws IOException 參數: str - String to be written Throws: IOException
- 無效寫入(字符串str,int關閉,intlen):寫入字符串的一部分。
Syntax :public void write(String str, int off, int len) throws IOException 參數: 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
相關用法
- Java Java.io.Writer.append()用法及代碼示例
- Java Java.io.Writer.close()用法及代碼示例
- Java Java.io.Writer.flush()用法及代碼示例
- Java Java.io.Writer.write()用法及代碼示例
- Java Java.io.BufferedInputStream.available()用法及代碼示例
- Java Java.io.BufferedInputStream.close()用法及代碼示例
- Java Java.io.BufferedInputStream.read()用法及代碼示例
- Java Java.io.BufferedInputStream.reset()用法及代碼示例
- Java Java.io.BufferedInputStream.skip()用法及代碼示例
- Java Java.io.BufferedOutputStream.flush()用法及代碼示例
- Java Java.io.BufferedOutputStream.Write()用法及代碼示例
- Java Java.io.BufferedReader.Close()用法及代碼示例
- Java Java.io.BufferedReader.mark()用法及代碼示例
- Java Java.io.BufferedReader.markSupported()用法及代碼示例
- Java Java.io.BufferedReader.read()用法及代碼示例
- Java Java.io.BufferedReader.readline()用法及代碼示例
- Java Java.io.BufferedReader.ready()用法及代碼示例
- Java Java.io.BufferedReader.reset()用法及代碼示例
- Java Java.io.BufferedReader.skip()用法及代碼示例
- Java Java.io.BufferedWriter.close()用法及代碼示例
- Java Java.io.BufferedWriter.flush()用法及代碼示例
- Java Java.io.BufferedWriter.newLine()用法及代碼示例
- Java Java.io.BufferedWriter.write()用法及代碼示例
- Java Java.io.ByteArrayInputStream.available()用法及代碼示例
- Java Java.io.ByteArrayInputStream.close()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.Writer class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。