java.io.writer的append和write方法有什么区别?
java.io.Writer 接口有两个方法,称为append和write 。这两者有什么区别?它甚至说
以
完全相同out.append(c)
形式调用此方法的行为与调用out.write(c)
所以有什么原因两个方法名称变体?
The java.io.Writer interface has two methods called append and write. What are the differences between these two? It even says that
An invocation of this method of the form
out.append(c)
behaves in exactly the same way as the invocationout.write(c)
so what is the reason for having two method name variants?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
append() 和 write() 之间存在细微差别。所有这些您都可以通过阅读 Javadocs 来解决。暗示。 ;)
void
write 是在 CharSequence 可用之前创建的较旧样式格式。
这些方法被重载,因此有一个
write(int)
,其中int
被强制转换为 char。append(char)
必须是 char 类型。write(char[] chars)
接受一个 char 数组,没有等效的append()。There are minor differences between append() and write(). All of which you can work out by reading the Javadocs. Hint. ;)
void
write is an older style format created before CharSequence was available.
These methods are overloaded so that there is a
write(int)
where theint
is cast to a char.append(char)
must be a char type.write(char[] chars)
takes an array of char, there is no equivalent append().可能是为了符合Appendable接口: http://download .oracle.com/javase/6/docs/api/java/lang/Appendable.html
Probably to conform to the Appendable interface: http://download.oracle.com/javase/6/docs/api/java/lang/Appendable.html
Append()
可以采用CharSequence
,而write()
则采用String
。由于
String
是CharSequence
的实现,因此您还可以将String
传递给append()
。但您也可以将StringBuilder
或StringBuffer
传递给append
,这是write()
无法做到的。Append()
can take aCharSequence
, whereaswrite()
takes aString
.Since
String
is an implementation ofCharSequence
, you can also pass aString
toappend()
. But you can also pass aStringBuilder
orStringBuffer
toappend
, which you can't do withwrite()
.从文档中可以看出,append 还会返回您刚刚写入的 Writer,以便您可以执行多个追加,例如:
As you can see from the documentation, append also returns the Writer you have just written to so that you can perform multiple appends such as:
Writer.append(c)
返回 Writer 实例。因此,您可以链接多个附加调用,例如out.append("Hello").append("World")
;Writer.append(c)
returns the Writer instance. Thus you can chain multiple calls to append, e.g.out.append("Hello").append("World")
;在我看来,它是
Appendable
接口的副产品,java.io.Writer
实现该接口是为了提供与java.util.Formatter
的兼容性。正如您所指出的,文档指出,对于 java.io.Writer,这两种方法之间没有实际区别。Looks to me like it's a byproduct of the
Appendable
interface whichjava.io.Writer
implements in order to provide compatibility withjava.util.Formatter
. As you noted, the documentation points out that forjava.io.Writer
there is no practical difference between the two methods.上面所指出的是真实的。
BufferWiter
的append()
方法的不同风格返回一个Writer
,它实现了Appendable
等,从而使您能够链式调用。此外,append()
允许您传递CharSequence
以及char
,而不是int
>、String
或char[]
,与write()
方法一样。全部正确。然而,正如问题所述,需要指出的一个重要问题是
append()
的底层行为仍然与write()
的基本行为相同。尽管有些误导性的命名法,现有的文件内容将被覆盖,不附加,除非您的FileWriter
被实例化:从这个意义上说,我认为最初的问题提出了一个有效的观点。
What has been noted above is true. The different flavours of
BufferWiter
'sappend()
method return aWriter
, which implementsAppendable
, among others, thereby affording you the ability to chain calls. In addition,append()
allows you to pass aCharSequence
as well as achar
, as opposed to anint
,String
orchar[]
, as with thewrite()
methods. All correct.However, as the question states, an important issue to point out that the underlying behaviour of
append()
is still much the same as that ofwrite()
. Despite the somewhat misleading nomenclature, existing file content will be overwritten, not appended, unless yourFileWriter
is instantiated otherwise:In that sense I think the original question raises a valid point.