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:
Spring RestTemplate Error Handling
Weak References in Java
Java Program to Implement Binary Search Tree
Comparing Objects in Java
Java Program to Implement Interpolation Search Algorithm
Java Program to Implement Sorted Array
Handle EML file with JavaMail
Java Program to Implement Bloom Filter
Java Program to Solve any Linear Equation in One Variable
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Using the Map.Entry Java Class
Xử lý ngoại lệ trong Java (Exception Handling)
Guide to the Synchronized Keyword in Java
Java Program to Permute All Letters of an Input String
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Generate Spring Boot REST Client with Swagger
Convert a Map to an Array, List or Set in Java
Guide to java.util.concurrent.BlockingQueue
Spring Boot - Creating Docker Image
Notify User of Login From New Device or Location
Migrating from JUnit 4 to JUnit 5
Dockerizing a Spring Boot Application
Jackson – Decide What Fields Get Serialized/Deserialized
Spring Data JPA and Null Parameters
Java Convenience Factory Methods for Collections
Hướng dẫn Java Design Pattern – Composite
Java Program to add two large numbers using Linked List
Validate email address exists or not by Java Code
Sắp xếp trong Java 8
Quick Guide on Loading Initial Data with Spring Boot
Registration – Password Strength and Rules
Build a REST API with Spring and Java Config