鴻蒙OS Writer

2022-10-25 17:38 更新

Writer

java.lang.Object

|---java.io.Writer

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

用于寫入字符流的抽象類。 子類必須實現(xiàn)的唯一方法是 write(char[], int, int)、flush() 和 close()。 然而,大多數(shù)子類將覆蓋此處定義的一些方法,以提供更高的效率、附加功能或兩者兼而有之。

Since:

JDK1.1

字段摘要

修飾符和類型 字段 描述
protected Object lock 用于同步此流上的操作的對象。

構造函數(shù)摘要

修飾符 構造函數(shù) 描述
protected Writer() 創(chuàng)建一個新的字符流編寫器,其關鍵部分將在編寫器本身上同步。
protected Writer(Object lock) 創(chuàng)建一個新的字符流編寫器,其關鍵部分將在給定對象上同步。

方法總結

修飾符和類型 方法 描述
Writer append(char c) 將指定的字符附加到這個 writer。
Writer append(CharSequence csq) 將指定的字符序列附加到此編寫器。
Writer append(CharSequence csq, int start, int end) 將指定字符序列的子序列附加到此編寫器。
abstract void close() 關閉流,首先刷新它。
abstract void flush() 沖洗流。
void write(char[] cbuf) 寫入一個字符數(shù)組。
abstract void write(char[] cbuf, int off, int len) 寫入字符數(shù)組的一部分。
void write(int c) 寫入單個字符。
void write(String str) 寫入一個字符串。
void write(String str, int off, int len) 寫入字符串的一部分。
從類 java.lang.Object 繼承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

字段詳細信息

lock

protected Object lock

用于同步此流上的操作的對象。 為了提高效率,字符流對象可以使用自身以外的對象來保護臨界區(qū)。 因此,子類應該使用該字段中的對象而不是 this 或同步方法。

構造函數(shù)詳細信息

Writer

protected Writer()

創(chuàng)建一個新的字符流編寫器,其關鍵部分將在編寫器本身上同步。

Writer

protected Writer(Object lock)

創(chuàng)建一個新的字符流編寫器,其關鍵部分將在給定對象上同步。

參數(shù):

參數(shù)名稱 參數(shù)描述
lock 要同步的對象

方法詳情

write

public void write(int c) throws IOException

寫入單個字符。 要寫入的字符包含在給定整數(shù)值的低 16 位中; 16 個高位被忽略。

打算支持高效單字符輸出的子類應覆蓋此方法。

參數(shù):

參數(shù)名稱 參數(shù)描述
c int 指定要寫入的字符

Throws:

Throw 名稱 Throw描述
IOException 如果發(fā)生 I/O 錯誤

write

public void write(char[] cbuf) throws IOException

寫入一個字符數(shù)組。

參數(shù):

參數(shù)名稱 參數(shù)描述
cbuf 要寫入的字符數(shù)組

Throws:

Throw名稱 Throw描述
IOException 如果發(fā)生 I/O 錯誤

write

public abstract void write(char[] cbuf, int off, int len) throws IOException

寫入字符數(shù)組的一部分。

參數(shù):

參數(shù)名稱 參數(shù)描述
cbuf 字符數(shù)組
off 開始寫入字符的偏移量
len 要寫入的字符數(shù)

Throws:

Throw名稱 Throw描述
IOException 如果發(fā)生 I/O 錯誤

write

public void write(String str) throws IOException

寫入一個字符串。

參數(shù):

參數(shù)名稱 參數(shù)描述
str 要寫入的字符串

Throws:

Throw名稱 Throw描述
IOException 如果發(fā)生 I/O 錯誤

write

public void write(String str, int off, int len) throws IOException

寫入字符串的一部分。

參數(shù):

參數(shù)名稱 參數(shù)描述
str 一個字符串
off 開始寫入字符的偏移量
len 要寫入的字符數(shù)

Throws:

Throw名稱 Throw描述
IndexOutOfBoundsException 如果 off 為負數(shù),或 len 為負數(shù),或 off+len 為負數(shù)或大于給定字符串的長度
IOException 如果發(fā)生 I/O 錯誤

append

public Writer append(CharSequence csq) throws IOException

將指定的字符序列附加到此編寫器。

形式為 out.append(csq) 的此方法的調用與調用的行為方式完全相同

     out.write(csq.toString()) 

根據字符序列 csq 的 toString 規(guī)范,可能不會附加整個序列。 例如,調用字符緩沖區(qū)的 toString 方法將返回一個子序列,其內容取決于緩沖區(qū)的位置和限制。

指定者:

在接口 Appendable 中追加

參數(shù):

參數(shù)名稱 參數(shù)描述
csq 要追加的字符序列。 如果 csq 為空,則將四個字符“null”附加到此編寫器。

返回:

This writer

Throws:

Throw名稱 Throw描述
IOException 如果發(fā)生 I/O 錯誤

Since:

1.5

append

public Writer append(CharSequence csq, int start, int end) throws IOException

將指定字符序列的子序列附加到此編寫器。 可附加的。

當 csq 不為空時,以 out.append(csq, start, end) 形式調用此方法的行為與調用完全相同

     out.write(csq.subSequence(start, end).toString()) 

指定者:

在接口 Appendable 中追加

參數(shù):

參數(shù)名稱 參數(shù)描述
csq 從中追加子序列的字符序列。 如果 csq 為 null,則將附加字符,就好像 csq 包含四個字符“null”。
start 子序列中第一個字符的索引
end 子序列中最后一個字符之后的字符的索引

返回:

This writer

Throws:

Throw名稱 Throw描述
IndexOutOfBoundsException 如果 start 或 end 為負數(shù),則 start 大于 end,或者 end 大于 csq.length()
IOException 如果發(fā)生 I/O 錯誤

Since:

1.5

append

public Writer append(char c) throws IOException

將指定的字符附加到這個 writer。

以 out.append(c) 形式調用此方法的行為與調用完全相同

     out.write(c) 

指定者:

在接口 Appendable 中追加

參數(shù):

參數(shù)名稱 參數(shù)描述
c 要附加的 16 位字符

返回:

This writer

Throws:

Throw名稱 Throw描述
IOException 如果發(fā)生 I/O 錯誤

Since:

1.5

flush

public abstract void flush() throws IOException

沖洗流。 如果流已將來自各種 write() 方法的任何字符保存在緩沖區(qū)中,請立即將它們寫入其預期目的地。 然后,如果該目的地是另一個字符或字節(jié)流,則刷新它。 因此,一次 flush() 調用將刷新 Writers 和 OutputStreams 鏈中的所有緩沖區(qū)。

如果此流的預期目的地是底層操作系統(tǒng)提供的抽象,例如文件,則刷新流僅保證先前寫入流的字節(jié)被傳遞給操作系統(tǒng)進行寫入; 它不能保證它們實際上被寫入了物理設備,例如磁盤驅動器。

指定者:

在接口 Flushable 中刷新

Throws:

Throw名稱 Throw描述
IOException 如果發(fā)生 I/O 錯誤

close

public abstract void close() throws IOException

關閉流,首先刷新它。 一旦流被關閉,進一步的 write() 或 flush() 調用將導致拋出 IOException。 關閉以前關閉的流沒有效果。

指定者:

在接口 AutoCloseable 中關閉

指定者:

在接口Closeable中關閉

Throws:

Throw名稱 Throw描述
IOException 如果發(fā)生 I/O 錯誤
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號