Posted In: IO, Java Core

How to create a Java string from the contents of a file

Techniques used

1. Before JDK 7 using BufferedReader
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.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.apache.commons.io.IOUtils;

public class ReadFileIntoString {

	public static void main(String[] args) throws Exception {
		String filePath = "E:/abhijit/db3.txt";
		// This code is without CHARSET and may create issue reading UTF files
		System.out.println(new ReadFileIntoString().readFile1(filePath));
		// This code is with CHARSET
		System.out.println(new ReadFileIntoString().readFile11(filePath));
		// JDK7 Files class
		System.out.println(new ReadFileIntoString().readFile2(filePath));
		// Apache commons IO Utils
		System.out.println(new ReadFileIntoString().readFile3(filePath));
		// Guava toString deprecated. This method is scheduled to be removed in
		// January 2019.
		System.out.println(new ReadFileIntoString().readFile4(filePath));
		// Guava asCharSource use this instead of toString
		System.out.println(new ReadFileIntoString().readFile5(filePath));
	}



	// This code is without CHARSET and may create issue reading UTF files
	private String readFile1(String file) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line = null;
		StringBuilder stringBuilder = new StringBuilder();
		String ls = System.getProperty("line.separator");

		try {
			while ((line = reader.readLine()) != null) {
				stringBuilder.append(line).append(ls);
			}

			return stringBuilder.toString();
		} finally {
			reader.close();
		}
	}




	// This code is with CHARSET
	private String readFile11(String file) throws IOException {
		File initialFile = new File(file);
		BufferedReader reader = new BufferedReader(new InputStreamReader(
		        new FileInputStream(initialFile), "UTF8"));
		String line = null;
		StringBuilder stringBuilder = new StringBuilder();
		String ls = System.getProperty("line.separator");

		try {
			while ((line = reader.readLine()) != null) {
				stringBuilder.append(line).append(ls);
			}

			return stringBuilder.toString();
		} finally {
			reader.close();
		}
	}



	// JDK7 Files class
	private String readFile2(String file) throws IOException {
		byte[] bytes = Files.readAllBytes(Paths.get(file));
		return new String(bytes, StandardCharsets.UTF_8);
	}



	// Apache commons IO Utils
	private String readFile3(String file) throws IOException {
		InputStream input = null;
		String str = null;
		// Convert File to InputStream
		try {
			File initialFile = new File(file);
			input = new FileInputStream(initialFile);
			str = IOUtils.toString(input, StandardCharsets.UTF_8);
		} finally {
			if (input != null)
				input.close();
		}

		return str;
	}



	// Guava toString deprecated. This method is scheduled to be removed in
	// January 2019.
	@SuppressWarnings({ "deprecation" })
	private String readFile4(String file) throws IOException {
		File initialFile = new File(file);
		return com.google.common.io.Files.toString(initialFile,
		        StandardCharsets.UTF_8);
	}



	// Guava asCharSource use this instead of toString
	private String readFile5(String file) throws IOException {
		File initialFile = new File(file);
		return com.google.common.io.Files
		        .asCharSource(initialFile, StandardCharsets.UTF_8).read();
	}
}

by , on August 22nd, 2017

  • Categories