Reading files in Java

16-10-2022
source code

Introduction

In this article I will go through various ways of reading a text file in Java.

Background

More often I found myself searching for ways to read file content into variables in Java unit tests. I thought to collect few methods of doing that for easy access next time I need it!

Setup

The project is a vanilla spring-boot starter app with nothing added to it. Out of the box you get a running application with skeleton unit test. The app starter was created using this website.

Using Java nio

Using File and Scanner

@Test
public void shouldReadFile_method1() throws FileNotFoundException {
  File myObj = new File(filePath);
  Scanner myReader = new Scanner(myObj);
  StringBuilder content = new StringBuilder();
  while (myReader.hasNextLine()) {
    content.append(myReader.nextLine());
    content.append('\n');
  }
  System.out.println(content);
  myReader.close();
}

Using BufferedReader

@Test
public void shouldReadFile_method2() throws IOException {
  BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
  StringBuilder content = new StringBuilder();
  String line = bufferedReader.readLine();

  while (line != null) {
    content.append(line);
    content.append(System.lineSeparator());
    line = bufferedReader.readLine();
  }
  bufferedReader.close();
  System.out.println(content);
}

One Liner

@Test
public void shouldReadFile_method3() throws IOException {
  String content = new String(Files.readAllBytes(Paths.get(filePath)));
  System.out.println(content);
}

@Test
public void shouldReadFile_method4() throws IOException {
  List content = Files.readAllLines(Paths.get(filePath));
  content.forEach(System.out::println);
}

@Test
public void shouldReadFile_method5() throws IOException {
  String content = Files.readString(Paths.get(filePath));
  System.out.println(content);
}

Using IOUtils

IOUtils comes from org.apache.commons.io package which you need to add to your gradle/maven.

@Test
public void shouldReadFile_method6() throws IOException {
  FileInputStream inputStream = new FileInputStream(filePath);
  String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
  inputStream.close();
  System.out.println(content);
}

@Test
public void shouldReadFile_method7() throws IOException {
  Collection content = IOUtils.readLines(new FileInputStream(filePath), StandardCharsets.UTF_8);
  content.forEach(System.out::println);
}

Which one to use?

Choosing a specific method to read file content into variables can depend on multiple factors. For example are you allowed to use external libraries in your project? if not then method6 and method7 is out of question.

Another factor is how big is the file? and how many times you need to read the file. If you have a couple of hundred unit tests reading files that contains few KB then any of these methods will do.

If however you are reading large files then speed is a very important factor. method5 is the fastest then method6 and method7. Please see attached image for running these tests on a text file that is 1MB in size. You can find this file under test/resources with the name sample-file-large.txt.

Project Structure