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:
Exploring the Spring Boot TestRestTemplate
Java Program to Implement Sorted Circular Doubly Linked List
Java Program to Implement Queue using Two Stacks
Java Program to Perform Partial Key Search in a K-D Tree
How to Get a Name of a Method Being Executed?
Spring Security with Maven
Java Program to implement Dynamic Array
Introduction to the Java NIO2 File API
An Intro to Spring Cloud Contract
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program to Implement Bubble Sort
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Java Program to Implement HashTable API
Java Program to Search for an Element in a Binary Search Tree
Java Program to Encode a Message Using Playfair Cipher
Java Program to Check the Connectivity of Graph Using DFS
Java Copy Constructor
A Guide to Java HashMap
How to Count Duplicate Elements in Arraylist
Anonymous Classes in Java
Jackson – Bidirectional Relationships
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Java Program to Find Strongly Connected Components in Graphs
Encode/Decode to/from Base64
Format ZonedDateTime to String
Introduction to Spring Cloud CLI
The Dining Philosophers Problem in Java
How to Return 404 with Spring WebFlux
Beans and Dependency Injection
Hướng dẫn sử dụng Lớp FilePermission trong java
Java Program to Implement Find all Forward Edges in a Graph
Đồng bộ hóa các luồng trong Java