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:

New Features in Java 10
Java Program to Implement LinkedHashSet API
Spring Boot - Quick Start
Java Program to Check whether Undirected Graph is Connected using DFS
Java Program to Implement vector
Getting Started with Stream Processing with Spring Cloud Data Flow
Java Program to Solve TSP Using Minimum Spanning Trees
Guide to the Volatile Keyword in Java
Guide to Java OutputStream
Lập trình đa luồng với CompletableFuture trong Java 8
Jackson – Decide What Fields Get Serialized/Deserialized
Dynamic Proxies in Java
Overflow and Underflow in Java
Java Program to Perform Cryptography Using Transposition Technique
Quick Intro to Spring Cloud Configuration
Java Program to Perform Arithmetic Operations on Numbers of Size
The Spring @Controller and @RestController Annotations
HttpClient 4 – Follow Redirects for POST
A Guide to TreeMap in Java
Java Program to Generate N Number of Passwords of Length M Each
New Stream Collectors in Java 9
Guide to WeakHashMap in Java
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Hướng dẫn Java Design Pattern – Adapter
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Implement ConcurrentHashMap API
The Thread.join() Method in Java
Spring Boot with Multiple SQL Import Files
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
A Quick JUnit vs TestNG Comparison
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Find Inverse of a Matrix