Java – File to Reader

In this quick tutorial we’re going to illustrate how to convert a File to a Reader using plain Java, Guava or Apache Commons IO. Let’s get started.

This article is part of the “Java – Back to Basic” series here on VietMX’s Blog.

1. With Plain Java

Let’s first look at the simple Java solution:

@Test
public void givenUsingPlainJava_whenConvertingFileIntoReader_thenCorrect() 
  throws IOException {
    File initialFile = new File("src/test/resources/initialFile.txt");
    initialFile.createNewFile();
    Reader targetReader = new FileReader(initialFile);
    targetReader.close();
}

2. With Guava

Now – let’s see the same conversion, this time using the Guava library:

@Test
public void givenUsingGuava_whenConvertingFileIntoReader_thenCorrect() throws 
  IOException {
    File initialFile = new File("src/test/resources/initialFile.txt");
    com.google.common.io.Files.touch(initialFile);
    Reader targetReader = Files.newReader(initialFile, Charset.defaultCharset());
    targetReader.close();
}

3. With Commons IO

And finally, let’s end with the Commons IO code sample, doing the conversion via an intermediary byte array:

@Test
public void givenUsingCommonsIO_whenConvertingFileIntoReader_thenCorrect() 
  throws IOException {
    File initialFile = new File("src/test/resources/initialFile.txt");
    FileUtils.touch(initialFile);
    FileUtils.write(initialFile, "With Commons IO");
    byte[] buffer = FileUtils.readFileToByteArray(initialFile);
    Reader targetReader = new CharSequenceReader(new String(buffer));
    targetReader.close();
}

And there we have it – 3 ways to convert a File into a Reader – first with plain Java, then with Guava and finally with the Apache Commons IO library. Make sure to check out the sample over on GitHub.

Related posts:

Remove All Occurrences of a Specific Value from a List
Spring Boot - Application Properties
Java Program to Implement Sparse Array
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to Implement Binomial Heap
Most commonly used String methods in Java
Java Program to Check whether Undirected Graph is Connected using BFS
Finding the Differences Between Two Lists in Java
How to Convert List to Map in Java
A Quick JUnit vs TestNG Comparison
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Jackson Exceptions – Problems and Solutions
Spring Boot - Code Structure
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Converting String to Stream of chars
Java Program to Implement Double Order Traversal of a Binary Tree
Introduction to Spring Boot CLI
Java Program to Perform Searching in a 2-Dimension K-D Tree
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
The Order of Tests in JUnit
Biến trong java
Java Program to Implement Johnson’s Algorithm
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Tạo chương trình Java đầu tiên sử dụng Eclipse
Biểu thức Lambda trong Java 8 – Lambda Expressions
Collection trong java
Tính trừu tượng (Abstraction) trong Java
So sánh HashMap và HashSet trong Java
Mệnh đề Switch-case trong java
Quick Guide to the Java StringTokenizer
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line