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 Threaded Binary Tree
Java Program to implement Bit Set
Hướng dẫn Java Design Pattern – Facade
Java Program to Implement Sorted Doubly Linked List
Guide to Guava Table
Redirect to Different Pages after Login with Spring Security
Java Program to Perform Uniform Binary Search
List Interface trong Java
Spring AMQP in Reactive Applications
Spring Boot Security Auto-Configuration
Using a Mutex Object in Java
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Java Program to Solve the Fractional Knapsack Problem
Receive email using IMAP
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Serverless Functions with Spring Cloud Function
Introduction to Java 8 Streams
Giới thiệu Google Guice – Dependency injection (DI) framework
Logout in an OAuth Secured Application
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Difference Between Wait and Sleep in Java
Java Program to Implement Control Table
Spring Boot - Build Systems
LIKE Queries in Spring JPA Repositories
Java Program to Implement Sieve Of Atkin
Spring NoSuchBeanDefinitionException
Java Program to Implement Attribute API
Getting Started with GraphQL and Spring Boot
Custom HTTP Header with the HttpClient
Send email with JavaMail