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:
Extract links from an HTML page
Một số ký tự đặc biệt trong Java
Java Program to Emulate N Dice Roller
Checking for Empty or Blank Strings in Java
Từ khóa throw và throws trong Java
Java Program to Construct an Expression Tree for an Postfix Expression
Overview of Spring Boot Dev Tools
Serialization và Deserialization trong java
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program to Describe the Representation of Graph using Adjacency List
Java Program to Implement Sorted Singly Linked List
New Features in Java 14
Spring Boot - Google OAuth2 Sign-In
How to Convert List to Map in Java
Spring Cloud Connectors and Heroku
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Using Spring ResponseEntity to Manipulate the HTTP Response
Spring NoSuchBeanDefinitionException
Netflix Archaius with Various Database Configurations
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Check whether Undirected Graph is Connected using BFS
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Create a Balanced Binary Tree of the Incoming Data
Java Program to Implement Bucket Sort
HandlerAdapters in Spring MVC
Zipping Collections in Java
Spring Boot - Web Socket
JUnit 5 for Kotlin Developers
Hướng dẫn Java Design Pattern – Service Locator
Giới thiệu thư viện Apache Commons Chain
Prevent Brute Force Authentication Attempts with Spring Security
Configure a RestTemplate with RestTemplateBuilder