Posted In: IO, Java Core

How to create a file and write text to it

Techniques used

1. Before JDK 7 using BufferedWriter

2. Using JDK 7 Files

3. Apache Commons IOUtils

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

4. Guava library

<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>23.0</version>
</dependency>

 

package com.example.java.core;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;

import org.apache.commons.io.IOUtils;
import org.junit.Test;

public class WriteTextIntoFile {

	String text = "write to file";


	@Test
	// This BufferedWriter code is without CHARSET
	public void writeFile1() throws IOException {
		String filePath = "E:/abhijit/file1.txt";
		BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));

		try {
			writer.write(text);
		} finally {
			writer.close();
		}
	}


	@Test
	// BufferedWriter code is with CHARSET
	public void writeFile2() throws IOException {
		String filePath = "E:/abhijit/file2.txt";
		File initialFile = new File(filePath);
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
		        new FileOutputStream(initialFile), "UTF8"));
		try {
			writer.write(text);
		} finally {
			writer.close();
		}
	}


	@Test
	// JDK7 Files class without CHARSET
	public void writeFile3a() throws IOException {
		String filePath = "E:/abhijit/file3a.txt";
		Files.write(Paths.get(filePath), text.getBytes(),
		        StandardOpenOption.CREATE_NEW);
	}


	@Test
	// JDK7 Files class with CHARSET
	public void writeFile3b() throws IOException {
		String filePath = "E:/abhijit/file3b.txt";
		Files.write(Paths.get(filePath), Arrays.asList(text),
		        Charset.forName("UTF-8"), StandardOpenOption.CREATE_NEW);
	}


	@Test
	// Apache commons IO Utils
	public void writeFile4() throws IOException {
		String filePath = "E:/abhijit/file4.txt";
		OutputStream output = null;

		try {
			File initialFile = new File(filePath);
			output = new FileOutputStream(initialFile);
			IOUtils.write(text.toCharArray(), output, StandardCharsets.UTF_8);
		} finally {
			if (output != null)
				output.close();
		}
	}


	@Test
	@SuppressWarnings("deprecation")
	// Guava write method is scheduled to be removed in January 2019.
	public void writeFile5() throws IOException {
		String filePath = "E:/abhijit/file5.txt";
		File initialFile = new File(filePath);
		com.google.common.io.Files.write(text, initialFile,
		        StandardCharsets.UTF_8);
	}


	@Test
	// Guava asCharSink use this instead of write
	public void writeFile6() throws IOException {
		String filePath = "E:/abhijit/file6.txt";
		File initialFile = new File(filePath);
		com.google.common.io.Files
		        .asCharSink(initialFile, StandardCharsets.UTF_8).write(text);
	}
}

by , on August 23rd, 2017

  • Categories