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:

Jackson – Change Name of Field
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Spring’s RequestBody and ResponseBody Annotations
Spring Boot - Apache Kafka
Java Program to Implement WeakHashMap API
A Guide to the Java LinkedList
Spring Data MongoDB – Indexes, Annotations and Converters
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
LinkedList trong java
Java Program to Perform Addition Operation Using Bitwise Operators
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Find Nearest Neighbor for Dynamic Data Set
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
JUnit 5 for Kotlin Developers
Guide to java.util.concurrent.Locks
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Summing Numbers with Java Streams
Hướng dẫn Java Design Pattern – State
Introduction to Spring Method Security
Creating a Custom Starter with Spring Boot
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Lớp Properties trong java
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Create a Custom Auto-Configuration with Spring Boot
Instance Profile Credentials using Spring Cloud
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Service Registration with Eureka
Java Program to Implement Binomial Heap
Java Program to Generate Random Hexadecimal Byte
Java Program to Implement Fibonacci Heap
Check If a File or Directory Exists in Java