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 Implement Max-Flow Min-Cut Theorem
Truyền giá trị và tham chiếu trong java
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Introduction to Spring MVC HandlerInterceptor
Simple Single Sign-On with Spring Security OAuth2
Introduction to PCollections
Using JWT with Spring Security OAuth
StringBuilder vs StringBuffer in Java
Java Program to Perform Insertion in a BST
Các nguyên lý thiết kế hướng đối tượng – SOLID
Spring Security 5 for Reactive Applications
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Java Program to Implement Queue using Linked List
String Initialization in Java
Biến trong java
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to Perform Arithmetic Operations on Numbers of Size
Spring WebFlux Filters
Java Program to Implement TreeSet API
Java Program to Implement Gauss Jordan Elimination
REST Web service: Upload và Download file với Jersey 2.x
Java 8 – Powerful Comparison with Lambdas
Multipart Upload with HttpClient 4
The Registration Process With Spring Security
Intro to Inversion of Control and Dependency Injection with Spring
An Intro to Spring Cloud Vault
Mockito and JUnit 5 – Using ExtendWith
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Implement Ternary Search Tree
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Spring Boot Annotations