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 Delete a Particular Node in a Tree Without Using Recursion
Introduction to the Java NIO Selector
Easy Ways to Write a Java InputStream to an OutputStream
Java Program to Construct an Expression Tree for an Postfix Expression
Kết hợp Java Reflection và Java Annotations
Java – InputStream to Reader
Spring RestTemplate Request/Response Logging
Java Program to Check whether Graph is a Bipartite using BFS
Java Program to Implement Insertion Sort
Java Program to Implement Word Wrap Problem
HttpClient 4 – Send Custom Cookie
Hướng dẫn sử dụng lớp Console trong java
Java program to Implement Tree Set
Java Program to find the number of occurrences of a given number using Binary Search approach
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Quick Guide to java.lang.System
Spring Security Authentication Provider
Java Program to Find Nearest Neighbor for Static Data Set
Tránh lỗi NullPointerException trong Java như thế nào?
Spring Boot - Actuator
Convert XML to JSON Using Jackson
Java Program to Implement Expression Tree
Marker Interface trong Java
Java Program to Implement Stack
Create a Custom Auto-Configuration with Spring Boot
Case-Insensitive String Matching in Java
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
StringBuilder vs StringBuffer in Java
Java Program to Implement Brent Cycle Algorithm
Phương thức forEach() trong java 8
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
The Difference Between map() and flatMap()