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:
Guide to Spring Cloud Kubernetes
REST Web service: Basic Authentication trong Jersey 2.x
Returning Image/Media Data with Spring MVC
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Spring Boot - Tomcat Deployment
Marker Interface trong Java
Display Auto-Configuration Report in Spring Boot
Java Program to Print only Odd Numbered Levels of a Tree
Java Program to Implement Fibonacci Heap
Java Program to Implement Best-First Search
So sánh ArrayList và LinkedList trong Java
Java Program to Implement Direct Addressing Tables
Ignore Null Fields with Jackson
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Instance Profile Credentials using Spring Cloud
Java Program to Implement Naor-Reingold Pseudo Random Function
Intro to Inversion of Control and Dependency Injection with Spring
Spring Boot: Customize the Jackson ObjectMapper
Allow user:password in URL
Chuyển đổi Array sang ArrayList và ngược lại
Java Program to implement Sparse Vector
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Control Structures in Java
Check if there is mail waiting
Java Program to Check for balanced parenthesis by using Stacks
Java Program to Implement Suffix Array
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Implement Ternary Search Algorithm
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Java InputStream to String
Java Program to Implement LinkedTransferQueue API