Posted In: IO, Java Core

How to create a Java List 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 java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;

public class ReadFileIntoList {

	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 ReadFileIntoList().readFile1(filePath));
		// This code is with CHARSET
		System.out.println(new ReadFileIntoList().readFile2(filePath));
		// JDK7 Files class
		System.out.println(new ReadFileIntoList().readFile3(filePath));
		// Apache commons IO Utils
		System.out.println(new ReadFileIntoList().readFile4(filePath));
		// Guava asCharSource use this instead of toString
		System.out.println(new ReadFileIntoList().readFile5(filePath));
	}

	// This code is without CHARSET and may create issue reading UTF files
	private List<String> readFile1(String file) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line = null;
		List<String> list = new ArrayList<String>();

		try {
			while ((line = reader.readLine()) != null) {
				list.add(line);
			}
			return list;
		} finally {
			reader.close();
		}
	}

	// This code is with CHARSET
	private List<String> readFile2(String file) throws IOException {
		File initialFile = new File(file);
		BufferedReader reader = new BufferedReader(new InputStreamReader(
		        new FileInputStream(initialFile), "UTF8"));
		String line = null;
		List<String> list = new ArrayList<String>();

		try {
			while ((line = reader.readLine()) != null) {
				list.add(line);
			}
			return list;
		} finally {
			reader.close();
		}
	}

	// JDK7 Files class
	private List<String> readFile3(String file) throws IOException {
		List<String> list = Files.readAllLines(Paths.get(file),
		        StandardCharsets.UTF_8);
		return list;
	}

	// Apache commons IO Utils
	private List<String> readFile4(String file) throws IOException {
		InputStream input = null;
		List<String> list = null;
		// Convert File to InputStream
		try {
			File initialFile = new File(file);
			input = new FileInputStream(initialFile);
			list = IOUtils.readLines(input, StandardCharsets.UTF_8);
		} finally {
			if (input != null)
				input.close();
		}

		return list;
	}

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

by , on August 22nd, 2017

  • Categories