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:
A Guide to Spring Boot Admin
@Order in Spring
Overview of the java.util.concurrent
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Hướng dẫn Java Design Pattern – Object Pool
wait() and notify() Methods in Java
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Java Program to Implement CopyOnWriteArrayList API
Java Program to Perform Stooge Sort
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Java Program to Implement Heap Sort Using Library Functions
Java Program to find the maximum subarray sum using Binary Search approach
Intro to the Jackson ObjectMapper
Read an Outlook MSG file
Ignore Null Fields with Jackson
Hướng dẫn Java Design Pattern – MVC
RestTemplate Post Request with JSON
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to Check Whether Graph is DAG
Java Program to Solve the 0-1 Knapsack Problem
Check if there is mail waiting
StringBuilder vs StringBuffer in Java
The Order of Tests in JUnit
Java Program to Implement Skew Heap
Java Program to Find Strongly Connected Components in Graphs
HashSet trong java
Java Program to Compute the Area of a Triangle Using Determinants
Hướng dẫn Java Design Pattern – Proxy
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to Represent Graph Using Linked List
Java Program to Create the Prufer Code for a Tree
Java Program to Find the Nearest Neighbor Using K-D Tree Search