Table of Contents
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:
Java Program to Implement Gauss Seidel Method
Spring Security with Maven
Guide to Escaping Characters in Java RegExps
Comparing Dates in Java
Java Program to Construct an Expression Tree for an Postfix Expression
Versioning a REST API
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Java Program to Implement DelayQueue API
Spring Security 5 for Reactive Applications
Runnable vs. Callable in Java
Java Program to add two large numbers using Linked List
A Guide to the ViewResolver in Spring MVC
Java Program to Represent Graph Using Adjacency List
Java Program to Implement Counting Sort
Spring Security Logout
Java Program to Implement SimpeBindings API
Guide to the Volatile Keyword in Java
Java Program to Find Minimum Element in an Array using Linear Search
Java Program to Perform Uniform Binary Search
Java Program to Implement Naor-Reingold Pseudo Random Function
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Java – Delete a File
So sánh HashMap và HashSet trong Java
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Java Program to Implement Disjoint Set Data Structure
Create a Custom Exception in Java
The Thread.join() Method in Java
Java 8 Stream API Analogies in Kotlin
Functional Interface trong Java 8
Java Program to Generate Random Numbers Using Middle Square Method
Java Program to Implement RoleUnresolvedList API
Java Program to Implement Stack