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 Find Strongly Connected Components in Graphs
Java Program to Check whether Undirected Graph is Connected using BFS
Java Program to Implement Singly Linked List
Show Hibernate/JPA SQL Statements from Spring Boot
Supplier trong Java 8
How to Get the Last Element of a Stream in Java?
Lớp HashMap trong Java
Java Program to Implement Doubly Linked List
Guide to @ConfigurationProperties in Spring Boot
Spring Data JPA @Modifying Annotation
Removing all Nulls from a List in Java
Binary Numbers in Java
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Java Program to Solve Knapsack Problem Using Dynamic Programming
Spring WebClient Requests with Parameters
Serialize Only Fields that meet a Custom Criteria with Jackson
Java Program to Perform String Matching Using String Library
A Guide to Spring Boot Admin
Tổng quan về ngôn ngữ lập trình java
Spring Security Remember Me
Converting String to Stream of chars
Intro to Inversion of Control and Dependency Injection with Spring
Guava CharMatcher
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
“Stream has already been operated upon or closed” Exception in Java
StringBuilder vs StringBuffer in Java
Java Program to Implement Ternary Heap
Tiêu chuẩn coding trong Java (Coding Standards)
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Spring Boot Security Auto-Configuration
Java InputStream to String
Guide to the Java TransferQueue