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:
Introduction to Project Reactor Bus
Spring Boot - Rest Template
Java Program to Create a Balanced Binary Tree of the Incoming Data
Logging a Reactive Sequence
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Pagination and Sorting using Spring Data JPA
Encode a String to UTF-8 in Java
Giới thiệu Design Patterns
Spring Boot - Unit Test Cases
Spring Boot - Enabling HTTPS
Tính đa hình (Polymorphism) trong Java
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Spring Boot - Tomcat Deployment
Java Program to Implement Binary Heap
Java Program to Implement Park-Miller Random Number Generation Algorithm
Simple Single Sign-On with Spring Security OAuth2
Send email with SMTPS (eg. Google GMail)
Java 8 Collectors toMap
Java Program to Find the GCD and LCM of two Numbers
Uploading MultipartFile with Spring RestTemplate
Count Occurrences of a Char in a String
Java Program to Find All Pairs Shortest Path
Guide to the Java TransferQueue
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Exploring the Spring Boot TestRestTemplate
Check If a File or Directory Exists in Java
Java Program to subtract two large numbers using Linked Lists
Java Program to Implement Kosaraju Algorithm
Java Program to Represent Graph Using 2D Arrays
Java Program to Create the Prufer Code for a Tree
Java Program to Implement Threaded Binary Tree