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:
Spring WebClient and OAuth2 Support
Java Program to Create a Random Graph Using Random Edge Generation
Java Program to Implement Sorted Circular Doubly Linked List
Iterable to Stream in Java
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Java Program to Implement vector
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Implement Quick sort
The Dining Philosophers Problem in Java
Guide to Selenium with JUnit / TestNG
JWT – Token-based Authentication trong Jersey 2.x
Exploring the Spring Boot TestRestTemplate
Tránh lỗi NullPointerException trong Java như thế nào?
Marker Interface trong Java
DynamoDB in a Spring Boot Application Using Spring Data
Hướng dẫn Java Design Pattern – Adapter
Java Program to Solve Tower of Hanoi Problem using Stacks
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Jackson vs Gson
Spring Data Java 8 Support
Java Program to Implement Double Ended Queue
Login For a Spring Web App – Error Handling and Localization
Introduction to Spring Data REST
Java Program to Check Cycle in a Graph using Topological Sort
CharSequence vs. String in Java
Java Program to Implement Uniform-Cost Search
Java Program to Solve a Matching Problem for a Given Specific Case
Logging a Reactive Sequence
Tính đa hình (Polymorphism) trong Java
Split a String in Java
Exception Handling in Java
Java Program to Implement Control Table