The FileUtils.writeStringToFile(fileName, text)
function of Apache Commons I/O overwrites previous text in a file. I would like to append data to my file. Is there any way I could use Commons I/O for the same? I can do it using normal BufferedWriter
from Java but I'm curious regarding the same using Commons I/O.
In Java, we can append a string in an existing file using FileWriter which has an option to open a file in append mode. Java FileWriter class is used to write character-oriented data to a file. It is a character-oriented class that is used for file handling in Java.
Apache Commons IO is a library of utilities to assist with developing IO functionality. There are six main areas included: io - This package defines utility classes for working with streams, readers, writers and files. comparator - This package provides various Comparator implementations for Files.
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 .
It has been implemented in 2.1 version of Apache IO. To append string to the file just pass true as an additional parameter in functions:
ex:
FileUtils.writeStringToFile(file, "String to append", true);
Download the latest version Commons-io 2.1
FileUtils.writeStringToFile(File,Data,append)
set append to true....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With