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:

Spring Boot - Code Structure
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Spring Boot: Customize the Jackson ObjectMapper
A Guide to LinkedHashMap in Java
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Tạo chương trình Java đầu tiên sử dụng Eclipse
Ép kiểu trong Java (Type casting)
Java Program to Implement Stack using Two Queues
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
HandlerAdapters in Spring MVC
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Implement Attribute API
Java Program to Implement Singly Linked List
Java Program to Implement the RSA Algorithm
Java Program to Implement Levenshtein Distance Computing Algorithm
The DAO with Spring and Hibernate
Iterating over Enum Values in Java
Từ khóa throw và throws trong Java
Bootstrap a Web Application with Spring 5
Java Program to Implement Binary Heap
Java Program to Implement Hash Tables Chaining with List Heads
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Java Program to Implement CountMinSketch
Getting a File’s Mime Type in Java
Spring Security Logout
Cachable Static Assets with Spring MVC
Java Program to Perform the Shaker Sort
Java Program to Implement Bellman-Ford Algorithm