34

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?

Svish
  • 152,914
  • 173
  • 462
  • 620

7 Answers7

42

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().
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Does that mean that I should use append rather than write? Why couldn't they just have called the overloads with CharSequence write as well and made the old write methods return the writer too? – Svish May 10 '11 at 12:28
  • 3
    @Svish, That would break backward compatibility. When code is compiled, the return type is part of the byte code signature and you wouldn't be able to use the new method without re-compiling the code from source. Also you may replace `write(int)` with `write(char)` in Java (which some code changes) but you definitive need to re-compile it. I would suggest you use `append` if you have no preference. The reason it has this name is to be consistent with StringBuffer which always used append. StringBuilder/StringBuffer/Writer all share the Appendable interface now. – Peter Lawrey May 10 '11 at 12:33
7

Probably to conform to the Appendable interface: http://download.oracle.com/javase/6/docs/api/java/lang/Appendable.html

jjwchoy
  • 1,898
  • 16
  • 20
5

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

Tom Jefferys
  • 13,090
  • 2
  • 35
  • 36
5

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)
David
  • 199
  • 1
  • 9
2

Writer.append(c) returns the Writer instance. Thus you can chain multiple calls to append, e.g. out.append("Hello").append("World");

aristotll
  • 8,694
  • 6
  • 33
  • 53
joe776
  • 1,106
  • 14
  • 23
1

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.

aroth
  • 54,026
  • 20
  • 135
  • 176
0

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.

cadebe
  • 651
  • 1
  • 12
  • 35