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 – Injecting Collections
Cài đặt và sử dụng Swagger UI
Guide to Java Instrumentation
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Phân biệt JVM, JRE, JDK
A Guide to the Java ExecutorService
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Quick Guide to Spring MVC with Velocity
Registration – Password Strength and Rules
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
How to Change the Default Port in Spring Boot
Convert Character Array to String in Java
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Configure a RestTemplate with RestTemplateBuilder
Java Program to Find All Pairs Shortest Path
New Features in Java 12
How to Break from Java Stream forEach
Java Program to Implement Flood Fill Algorithm
Spring Boot - Apache Kafka
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
A Guide to EnumMap
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Immutable ArrayList in Java
Java Program to Implement Leftist Heap
Spring Boot - Tomcat Port Number
Mảng (Array) trong Java
Java Program to Implement LinkedBlockingDeque API
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Perform Insertion in a BST
Lớp TreeMap trong Java
Guide to WeakHashMap in Java
Map Serialization and Deserialization with Jackson