codecamp

鸿蒙OS Writer

Writer

java.lang.Object

|---java.io.Writer

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

用于写入字符流的抽象类。 子类必须实现的唯一方法是 write(char[], int, int)、flush() 和 close()。 然而,大多数子类将覆盖此处定义的一些方法,以提供更高的效率、附加功能或两者兼而有之。

Since:

JDK1.1

字段摘要

修饰符和类型 字段 描述
protected Object lock 用于同步此流上的操作的对象。

构造函数摘要

修饰符 构造函数 描述
protected Writer() 创建一个新的字符流编写器,其关键部分将在编写器本身上同步。
protected Writer(Object lock) 创建一个新的字符流编写器,其关键部分将在给定对象上同步。

方法总结

修饰符和类型 方法 描述
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) 写入一个字符数组。
abstract void write(char[] cbuf, int off, int len) 写入字符数组的一部分。
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

用于同步此流上的操作的对象。 为了提高效率,字符流对象可以使用自身以外的对象来保护临界区。 因此,子类应该使用该字段中的对象而不是 this 或同步方法。

构造函数详细信息

Writer

protected Writer()

创建一个新的字符流编写器,其关键部分将在编写器本身上同步。

Writer

protected Writer(Object lock)

创建一个新的字符流编写器,其关键部分将在给定对象上同步。

参数:

参数名称 参数描述
lock 要同步的对象

方法详情

write

public void write(int c) throws IOException

写入单个字符。 要写入的字符包含在给定整数值的低 16 位中; 16 个高位被忽略。

打算支持高效单字符输出的子类应覆盖此方法。

参数:

参数名称 参数描述
c int 指定要写入的字符

Throws:

Throw 名称 Throw描述
IOException 如果发生 I/O 错误

write

public void write(char[] cbuf) throws IOException

写入一个字符数组。

参数:

参数名称 参数描述
cbuf 要写入的字符数组

Throws:

Throw名称 Throw描述
IOException 如果发生 I/O 错误

write

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

写入字符数组的一部分。

参数:

参数名称 参数描述
cbuf 字符数组
off 开始写入字符的偏移量
len 要写入的字符数

Throws:

Throw名称 Throw描述
IOException 如果发生 I/O 错误

write

public void write(String str) throws IOException

写入一个字符串。

参数:

参数名称 参数描述
str 要写入的字符串

Throws:

Throw名称 Throw描述
IOException 如果发生 I/O 错误

write

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

写入字符串的一部分。

参数:

参数名称 参数描述
str 一个字符串
off 开始写入字符的偏移量
len 要写入的字符数

Throws:

Throw名称 Throw描述
IndexOutOfBoundsException 如果 off 为负数,或 len 为负数,或 off+len 为负数或大于给定字符串的长度
IOException 如果发生 I/O 错误

append

public Writer append(CharSequence csq) throws IOException

将指定的字符序列附加到此编写器。

形式为 out.append(csq) 的此方法的调用与调用的行为方式完全相同

     out.write(csq.toString()) 

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

指定者:

在接口 Appendable 中追加

参数:

参数名称 参数描述
csq 要追加的字符序列。 如果 csq 为空,则将四个字符“null”附加到此编写器。

返回:

This writer

Throws:

Throw名称 Throw描述
IOException 如果发生 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 中追加

参数:

参数名称 参数描述
csq 从中追加子序列的字符序列。 如果 csq 为 null,则将附加字符,就好像 csq 包含四个字符“null”。
start 子序列中第一个字符的索引
end 子序列中最后一个字符之后的字符的索引

返回:

This writer

Throws:

Throw名称 Throw描述
IndexOutOfBoundsException 如果 start 或 end 为负数,则 start 大于 end,或者 end 大于 csq.length()
IOException 如果发生 I/O 错误

Since:

1.5

append

public Writer append(char c) throws IOException

将指定的字符附加到这个 writer。

以 out.append(c) 形式调用此方法的行为与调用完全相同

     out.write(c) 

指定者:

在接口 Appendable 中追加

参数:

参数名称 参数描述
c 要附加的 16 位字符

返回:

This writer

Throws:

Throw名称 Throw描述
IOException 如果发生 I/O 错误

Since:

1.5

flush

public abstract void flush() throws IOException

冲洗流。 如果流已将来自各种 write() 方法的任何字符保存在缓冲区中,请立即将它们写入其预期目的地。 然后,如果该目的地是另一个字符或字节流,则刷新它。 因此,一次 flush() 调用将刷新 Writers 和 OutputStreams 链中的所有缓冲区。

如果此流的预期目的地是底层操作系统提供的抽象,例如文件,则刷新流仅保证先前写入流的字节被传递给操作系统进行写入; 它不能保证它们实际上被写入了物理设备,例如磁盘驱动器。

指定者:

在接口 Flushable 中刷新

Throws:

Throw名称 Throw描述
IOException 如果发生 I/O 错误

close

public abstract void close() throws IOException

关闭流,首先刷新它。 一旦流被关闭,进一步的 write() 或 flush() 调用将导致抛出 IOException。 关闭以前关闭的流没有效果。

指定者:

在接口 AutoCloseable 中关闭

指定者:

在接口Closeable中关闭

Throws:

Throw名称 Throw描述
IOException 如果发生 I/O 错误
鸿蒙OS StringWriter
鸿蒙OS CharConversionException
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

鸿蒙OS 开发

鸿蒙OS 术语

鸿蒙OS Java API参考

鸿蒙OS ohos.aafwk.ability

鸿蒙OS ohos.aafwk.abilityjet.activedata

鸿蒙OS ohos.aafwk.content

鸿蒙OS java.lang

鸿蒙OS java.Util

鸿蒙OS java.Util class

鸿蒙OS ohos.data.dataability

鸿蒙OS ohos.data.dataability class

鸿蒙OS ohos.agp.components

鸿蒙OS ohos.agp.components interface

鸿蒙OS ohos.agp.components class

鸿蒙OS ohos.global.configuration

鸿蒙OS java.io

鸿蒙OS ohos.data.resultset

鸿蒙OS ohos.data.resultset interface

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }