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:
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement Sorted Doubly Linked List
Inheritance with Jackson
Join and Split Arrays and Collections in Java
Java Program to find the peak element of an array using Binary Search approach
Chuyển đổi Array sang ArrayList và ngược lại
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Iterating over Enum Values in Java
Guide to PriorityBlockingQueue in Java
Rate Limiting in Spring Cloud Netflix Zuul
Convert a Map to an Array, List or Set in Java
Chuyển đổi giữa các kiểu dữ liệu trong Java
Using a Mutex Object in Java
Java 9 Stream API Improvements
String Operations with Java Streams
Spring Data Java 8 Support
JUnit 5 for Kotlin Developers
Spring AMQP in Reactive Applications
Java 8 and Infinite Streams
Java Program to Implement Flood Fill Algorithm
How to Replace Many if Statements in Java
HashSet trong Java hoạt động như thế nào?
Guide to the Java ArrayList
Java Program to Implement Segment Tree
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Luồng Daemon (Daemon Thread) trong Java
Java Program to Implement Hash Trie
Phương thức forEach() trong java 8
Configuring a DataSource Programmatically in Spring Boot
Java Program to Implement Heap Sort Using Library Functions
Default Password Encoder in Spring Security 5