Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between append and write methods of java.io.writer?

Tags:

java

writer

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?

like image 481
Svish Avatar asked May 10 '11 12:05

Svish


People also ask

What is the difference between write and append?

Solution. The write mode creates a new file. append mode is used to add the data at the end of the file if the file already exists. If the file is already existing write mode overwrites it.

What is the difference between BufferedWriter and FileWriter?

FileWriter writes directly into Files and should be used only when the number of writes is less. BufferedWriter: BufferedWriter is almost similar to FileWriter but it uses internal buffer to write data into File. So if the number of write operations is more, the actual IO operations are less and performance is better.

Does FileWriter append?

FileWriter takes an optional second parameter: append . If set to true, then the data will be written to the end of the file. This example appends data to a file with FileWriter .

What does writer do in Java?

The Writer class of the java.io package is an abstract superclass that represents a stream of characters. Since Writer is an abstract class, it is not useful by itself. However, its subclasses can be used to write data.


Video Answer


1 Answers

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().
like image 142
Peter Lawrey Avatar answered Oct 06 '22 04:10

Peter Lawrey