|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
java.lang.Objectjava.io.Writer
java.io.BufferedWriter
public class BufferedWriter
將文本寫入字元輸出串流,緩衝各個字元,從而提供單個字元、陣列和字元串的高效寫入。
可以指定緩衝區的大小,或者接受預設的大小。在大多數情況下,預設值就足夠大了。
該類別提供了 newLine() 方法,它使用平臺自己的行分隔符概念,此概念由系統屬性 line.separator 定義。並非所有平臺都使用新行符 ('\n') 來終止各行。因此調用此方法來終止每個輸出行要優於直接寫入新行符。
通常 Writer 將其輸出立即發送到底層字元或位元組串流。除非要求提示輸出,否則建議用 BufferedWriter 套件裝所有其 write() 操作可能開銷很高的 Writer(如 FileWriters 和 OutputStreamWriters)。例如,
PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
將緩衝 PrintWriter 對檔案的輸出。如果沒有緩衝,則每次調用 print() 方法會導致將字元轉換為位元組,然後立即寫入到檔案,而這是極其低效的。
PrintWriter,
FileWriter,
OutputStreamWriter| 欄位摘要 |
|---|
| 從類別 java.io.Writer 繼承的欄位 |
|---|
lock |
| 建構子摘要 | |
|---|---|
BufferedWriter(Writer out)
創建一個使用預設大小輸出緩衝區的緩衝字元輸出串流。 |
|
BufferedWriter(Writer out,
int sz)
創建一個使用給定大小輸出緩衝區的新緩衝字元輸出串流。 |
|
| 方法摘要 | |
|---|---|
void |
close()
關閉此串流,但要先刷新它。 |
void |
flush()
刷新該串流的緩衝。 |
void |
newLine()
寫入一個行分隔符。 |
void |
write(char[] cbuf,
int off,
int len)
寫入字元陣列的某一部分。 |
void |
write(int c)
寫入單個字元。 |
void |
write(String s,
int off,
int len)
寫入字元串的某一部分。 |
| 從類別 java.io.Writer 繼承的方法 |
|---|
append, append, append, write, write |
| 從類別 java.lang.Object 繼承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| 建構子詳細資訊 |
|---|
public BufferedWriter(Writer out)
out - 一個 Writer
public BufferedWriter(Writer out,
int sz)
out - 一個 Writersz - 輸出緩衝區的大小,是一個正整數
IllegalArgumentException - 如果 sz <= 0| 方法詳細資訊 |
|---|
public void write(int c)
throws IOException
Writer 中的 writec - 指定要寫入字元的 int。
IOException - 如果發生 I/O 錯誤
public void write(char[] cbuf,
int off,
int len)
throws IOException
一般來說,此方法將給定陣列的字元存入此串流的緩衝區中,根據需要刷新該緩衝區,並轉到底層串流。但是,如果請求的長度至少與此緩衝區大小相同,則此方法將刷新該緩衝區並將各個字元直接寫入底層串流。因此多餘的 BufferedWriter 將不必複製資料。
Writer 中的 writecbuf - 字元陣列off - 開始讀取字元處的偏移量len - 要寫入的字元數
IOException - 如果發生 I/O 錯誤
public void write(String s,
int off,
int len)
throws IOException
如果 len 參數的值為負數,則不寫入任何字元。這與父級類別中此方法的規範正好相反,它要求拋出 IndexOutOfBoundsException。
Writer 中的 writes - 要寫入的字元串off - 開始讀取字元處的偏移量len - 要寫入的字元數
IOException - 如果發生 I/O 錯誤
public void newLine()
throws IOException
IOException - 如果發生 I/O 錯誤
public void flush()
throws IOException
Flushable 中的 flushWriter 中的 flushIOException - 如果發生 I/O 錯誤
public void close()
throws IOException
Writer 複製的描述
Closeable 中的 closeWriter 中的 closeIOException - 如果發生 I/O 錯誤
|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
版權所有 2008 Sun Microsystems, Inc. 保留所有權利。請遵守GNU General Public License, version 2 only。