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:
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Perform the Sorting Using Counting Sort
Spring Boot - Google OAuth2 Sign-In
Migrating from JUnit 4 to JUnit 5
Từ khóa throw và throws trong Java
Java Program to Encode a Message Using Playfair Cipher
Java Program to Implement Randomized Binary Search Tree
Merging Streams in Java
The Difference Between Collection.stream().forEach() and Collection.forEach()
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Giới thiệu SOAP UI và thực hiện test Web Service
Setting Up Swagger 2 with a Spring REST API
Apache Tiles Integration with Spring MVC
Transaction Propagation and Isolation in Spring @Transactional
Implementing a Binary Tree in Java
The Modulo Operator in Java
Introduction to Spring Data JDBC
JUnit 5 @Test Annotation
Explain about URL and HTTPS protocol
Java Program to Implement Stack using Two Queues
Java Program to Implement Euclid GCD Algorithm
How to Break from Java Stream forEach
Spring Cloud AWS – EC2
Java Program to Implement Uniform-Cost Search
Mảng (Array) trong Java
Convert String to int or Integer in Java
Guava – Join and Split Collections
Mockito and JUnit 5 – Using ExtendWith
Chuyển đổi Array sang ArrayList và ngược lại
Quick Guide on Loading Initial Data with Spring Boot
Java Program to Implement Repeated Squaring Algorithm