Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append data into a file using Apache Commons I/O

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.

like image 763
Dexter Avatar asked Jun 03 '10 13:06

Dexter


People also ask

How do you append data to an existing file in Java?

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.

What is Apache Commons IO used for?

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.

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 .


2 Answers

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:

  • FileUtils.writeStringToFile
  • FileUtils.openOutputStream
  • FileUtils.write
  • FileUtils.writeByteArrayToFile
  • FileUtils.writeLines

ex:

    FileUtils.writeStringToFile(file, "String to append", true); 
like image 53
JJ Roman Avatar answered Oct 13 '22 22:10

JJ Roman


Download the latest version Commons-io 2.1

FileUtils.writeStringToFile(File,Data,append) 

set append to true....

like image 27
Makky Avatar answered Oct 13 '22 23:10

Makky