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:
A Guide to EnumMap
Java Program to Implement Singly Linked List
Merging Streams in Java
Java Program to Implement Affine Cipher
Biến trong java
Guide To CompletableFuture
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
LIKE Queries in Spring JPA Repositories
Derived Query Methods in Spring Data JPA Repositories
Intersection of Two Lists in Java
Receive email using POP3
Introduction to Apache Commons Text
Java Program to Implement Disjoint Set Data Structure
Spring Autowiring of Generic Types
Display Auto-Configuration Report in Spring Boot
Java 8 Stream findFirst() vs. findAny()
Hướng dẫn Java Design Pattern – Prototype
Spring Boot - Service Components
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Java Program to Check if it is a Sparse Matrix
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Predicate trong Java 8
Using Spring ResponseEntity to Manipulate the HTTP Response
Convert String to Byte Array and Reverse in Java
Immutable Objects in Java
Java Program to Generate All Possible Combinations of a Given List of Numbers
Guide to CopyOnWriteArrayList
Java Program to Implement Park-Miller Random Number Generation Algorithm
Spring Boot - Internationalization
Java Program to Implement Find all Forward Edges in a Graph
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Convert Character Array to String in Java