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 Rolling Hash
Java Program to Implement HashMap API
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Java Program to Find the GCD and LCM of two Numbers
Configure a Spring Boot Web Application
Predicate trong Java 8
How to Manually Authenticate User with Spring Security
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Biểu thức Lambda trong Java 8 – Lambda Expressions
Simple Single Sign-On with Spring Security OAuth2
Setting a Request Timeout for a Spring REST API
Java Program to Find Nearest Neighbor Using Linear Search
Introduction to Eclipse Collections
Merging Two Maps with Java 8
Getting Started with GraphQL and Spring Boot
Guide to ThreadLocalRandom in Java
HttpClient 4 – Follow Redirects for POST
Guide to @JsonFormat in Jackson
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Java Program to Implement ConcurrentSkipListMap API
Spring Boot - Scheduling
Life Cycle of a Thread in Java
Java Program to Implement Sorted Singly Linked List
New Features in Java 15
Spring WebClient Filters
Apache Tiles Integration with Spring MVC
Java Program to Implement Sorted Circularly Singly Linked List
Spring Data Reactive Repositories with MongoDB
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Java Program to Implement Binomial Tree
RestTemplate Post Request with JSON
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?