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:
Quick Guide to java.lang.System
Introduction to Spring Cloud Netflix – Eureka
Introduction to the Java ArrayDeque
Java Program to Implement Selection Sort
Java Program to Implement Bucket Sort
Java Program to implement Sparse Vector
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Hướng dẫn sử dụng Java Reflection
Primitive Type Streams in Java 8
Spring WebClient and OAuth2 Support
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to Implement Fermat Factorization Algorithm
LIKE Queries in Spring JPA Repositories
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Java Program to Implement Min Heap
The Spring @Controller and @RestController Annotations
A Guide to the Java LinkedList
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Convert Character Array to String in Java
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Biểu thức Lambda trong Java 8 – Lambda Expressions
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Implementing a Binary Tree in Java
Java Program to Permute All Letters of an Input String
Java Program to Implement Depth-limited Search
Jackson – Unmarshall to Collection/Array
Immutable ArrayList in Java
How to Kill a Java Thread
Java Program to Implement Trie
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Guide to DelayQueue