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:
Mockito and JUnit 5 – Using ExtendWith
Java Program to Perform Stooge Sort
Basic Authentication with the RestTemplate
Java Program to Implement Binary Heap
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Java Web Services – JAX-WS – SOAP
Using the Map.Entry Java Class
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Hướng dẫn Java Design Pattern – Visitor
HttpClient 4 – Send Custom Cookie
Java Program to Implement the String Search Algorithm for Short Text Sizes
Java Program to implement Bit Matrix
Jackson vs Gson
Hướng dẫn Java Design Pattern – Observer
Java Program to Implement Ford–Fulkerson Algorithm
Java Program to Implement AttributeList API
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Implement Queue using Two Stacks
Java Program to Implement Threaded Binary Tree
Java Program to Implement Sparse Matrix
Java Program to Describe the Representation of Graph using Incidence Matrix
Spring Boot - Runners
A Guide to the finalize Method in Java
Spring WebClient vs. RestTemplate
Explain about URL and HTTPS protocol
A Guide to JPA with Spring
Serve Static Resources with Spring
Getting Started with Forms in Spring MVC
Từ khóa static và final trong java
Java Program to Implement HashSet API
Java Program to Implement Direct Addressing Tables