Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

Best Way to Append Data to File in Java – Apache commons.io.FileUtils and java.io.BufferedWriter Approach

$
0
0

What is the best way to append live data to a file in Java? It’s very easy for developer to print output or some debug data to Eclipse Console using println(). But what if you would like to save data for later use?

Well, in Java you could use log4.xml file file to store log statements to specific file location. But in this tutorial we will go over two approaches to save data to file.

Let’s take a look at all below queries:

  • Java – How to append content to file
  • How to append text to an existing file in Java
  • Java Examples – Append String to a File

Approach 1: Using BufferedWriter

BufferedWriter is a character streams class. It writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

Approach 2: Apache Commons IO

FileUtils.writeStringToFile writes a String to a file creating the file if it does not exist.

You need below maven dependency for this approach.

<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.4</version>
</dependency>

Let’s get started:

  1. Create class CrunchifyApacheCommonFileAppend.java
  2. Create two methods writeToFileBufferedWriter and writeToFileApacheCommonIO
  3. Call above two methods 2 times with different statements from main()
  4. Checkout file /users/ashah/Downloads/crunchify.txt

package crunchify.com.tutorials;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

/**
 * @author Crunchify.com 
 * Crunchify's Append data to File Example
 */

public class CrunchifyApacheCommonFileAppend {
	static File file = new File("/users/ashah/Downloads/crunchify.txt");
	static String fileContent = "This is my 1st message - BufferedWriter Approach \n";
	static String fileContent2 = "This is my 2nd message - Apache Common IO Approach \n";

	public static void main(String[] args) {

		// Method1: Using BufferedWriter - character streams class
		writeToFileBufferedWriter(fileContent);
		writeToFileBufferedWriter(fileContent2);

		// Method2: Using Apache Commons IO
		writeToFileApacheCommonIO(fileContent);
		writeToFileApacheCommonIO(fileContent2);

		System.out.println("File Updated.");
	}

	public static void writeToFileBufferedWriter(String msg) {
		FileWriter fileWriter;
		BufferedWriter bufferedWriter;
		try {
			fileWriter = new FileWriter(file.getAbsoluteFile(), true); // true to append
			bufferedWriter = new BufferedWriter(fileWriter);
			bufferedWriter.write(msg);
			bufferedWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void writeToFileApacheCommonIO(String msg) {
		try {
			// 3rd parameter boolean append = true
			FileUtils.writeStringToFile(file, msg, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Now just check it out file crunchify.txt

File append example result - crunchify

The post Best Way to Append Data to File in Java – Apache commons.io.FileUtils and java.io.BufferedWriter Approach appeared first on Crunchify.


Viewing all articles
Browse latest Browse all 1037

Trending Articles