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:
Tổng quan về ngôn ngữ lập trình java
Function trong Java 8
Object Type Casting in Java
Spring Boot - Interceptor
Introduction to Spring Cloud Netflix – Eureka
Java Program to Implement Leftist Heap
Tránh lỗi NullPointerException trong Java như thế nào?
Handle EML file with JavaMail
Jackson Annotation Examples
Java Program to Implement Sorted Array
Hướng dẫn Java Design Pattern – Facade
Collect a Java Stream to an Immutable Collection
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to find the peak element of an array using Binary Search approach
Java Program to Find the Vertex Connectivity of a Graph
Filtering and Transforming Collections in Guava
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Java Program to Compute Determinant of a Matrix
Encode/Decode to/from Base64
Java Program to Check Cycle in a Graph using Graph traversal
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Convert char to String in Java
HashSet trong java
Java Copy Constructor
Java Program to Implement CountMinSketch
The Java 8 Stream API Tutorial
Java Program to Find a Good Feedback Edge Set in a Graph
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Implement Binary Tree
Spring Autowiring of Generic Types
Java – InputStream to Reader
Basic Authentication with the RestTemplate