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 Perform Inorder Recursive Traversal of a Given Binary Tree
Spring Data – CrudRepository save() Method
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Working with Tree Model Nodes in Jackson
Spring Boot - Introduction
Jackson Date
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Implement Levenshtein Distance Computing Algorithm
Mapping Nested Values with Jackson
Java – Reader to InputStream
Java Program to Implement Expression Tree
Spring Data Java 8 Support
An Intro to Spring Cloud Zookeeper
Hướng dẫn Java Design Pattern – Observer
Java Program to Implement Weight Balanced Tree
How to Break from Java Stream forEach
Java Program to Implement Bit Array
Guide to BufferedReader
A Guide to JUnit 5
Concrete Class in Java
Java Program to Implement Skip List
Guide to Java 8 groupingBy Collector
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Introduction to Spring Cloud Stream
Java Program to Generate N Number of Passwords of Length M Each
Java Program to Represent Graph Using Linked List
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Hướng dẫn Java Design Pattern – MVC
Java Program to Find Whether a Path Exists Between 2 Given Nodes
A Quick JUnit vs TestNG Comparison
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Các nguyên lý thiết kế hướng đối tượng – SOLID