java.io.writer的append和write方法有什么区别?

发布于 2024-11-05 20:10:15 字数 274 浏览 0 评论 0原文

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 invocation out.write(c)

so what is the reason for having two method name variants?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

心舞飞扬 2024-11-12 20:10:15

append() 和 write() 之间存在细微差别。所有这些您都可以通过阅读 Javadocs 来解决。暗示。 ;)

  • write 只会接受一个不能为 null 的 String 并返回 void
  • append 将接受任何可以为 null 的 CharSequence 并返回 Writer,以便可以将其链接起来。

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. ;)

  • write will only take a String which must not be null and returns void
  • append will take any CharSequence which can be null and return the Writer so it can be chained.

write is an older style format created before CharSequence was available.

These methods are overloaded so that there is a

  • write(int) where the int 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().
对岸观火 2024-11-12 20:10:15

Append() 可以采用 CharSequence,而 write() 则采用 String

由于 StringCharSequence 的实现,因此您还可以将 String 传递给 append()。但您也可以将 StringBuilderStringBuffer 传递给 append,这是 write() 无法做到的。

Append() can take a CharSequence, whereas write() takes a String.

Since String is an implementation of CharSequence, you can also pass a String to append(). But you can also pass a StringBuilder or StringBuffer to append, which you can't do with write().

流年已逝 2024-11-12 20:10:15

从文档中可以看出,append 还会返回您刚刚写入的 Writer,以便您可以执行多个追加,例如:

out.append(a).append(b).append(c)

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:

out.append(a).append(b).append(c)
浪菊怪哟 2024-11-12 20:10:15

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");

芸娘子的小脾气 2024-11-12 20:10:15

在我看来,它是 Appendable 接口的副产品,java.io.Writer 实现该接口是为了提供与 java.util.Formatter 的兼容性。正如您所指出的,文档指出,对于 java.io.Writer,这两种方法之间没有实际区别。

Looks to me like it's a byproduct of the Appendable interface which java.io.Writer implements in order to provide compatibility with java.util.Formatter. As you noted, the documentation points out that for java.io.Writer there is no practical difference between the two methods.

莫相离 2024-11-12 20:10:15

上面所指出的是真实的。 BufferWiterappend() 方法的不同风格返回一个 Writer,它实现了 Appendable 等,从而使您能够链式调用。此外,append() 允许您传递 CharSequence 以及 char,而不是 int >、Stringchar[],与 write() 方法一样。全部正确。

然而,正如问题所述,需要指出的一个重要问题是 append() 的底层行为仍然与 write() 的基本行为相同。尽管有些误导性的命名法,现有的文件内容将被覆盖,不附加,除非您的 FileWriter 被实例化:

try (var writer = new BufferedWriter(new FileWriter("myFile.txt", true));) {
    writer.append("XXX").append("ZZZ");
}

从这个意义上说,我认为最初的问题提出了一个有效的观点。

What has been noted above is true. The different flavours of BufferWiter's append() method return a Writer, which implements Appendable, among others, thereby affording you the ability to chain calls. In addition, append() allows you to pass a CharSequence as well as a char, as opposed to an int, String or char[], as with the write() 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 of write(). Despite the somewhat misleading nomenclature, existing file content will be overwritten, not appended, unless your FileWriter is instantiated otherwise:

try (var writer = new BufferedWriter(new FileWriter("myFile.txt", true));) {
    writer.append("XXX").append("ZZZ");
}

In that sense I think the original question raises a valid point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文