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 Perform Arithmetic Operations on Numbers of Size
Simultaneous Spring WebClient Calls
Sử dụng CountDownLatch trong Java
Java Program to Implement Pagoda
HashMap trong Java hoạt động như thế nào?
Split a String in Java
Convert a Map to an Array, List or Set in Java
Java Program to Implement Dijkstra’s Algorithm using Set
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Java Program to Implement Adjacency Matrix
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Query Entities by Dates and Times with Spring Data JPA
Spring Web Annotations
Spring WebClient Filters
Custom Exception trong Java
Java Program to Implement Sparse Matrix
Java Convenience Factory Methods for Collections
Format ZonedDateTime to String
Java Program to Perform Partition of an Integer in All Possible Ways
Allow user:password in URL
The Java 8 Stream API Tutorial
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Java Program to Implement Interpolation Search Algorithm
Java Program to Represent Graph Using Incidence Matrix
Java Program to find the maximum subarray sum using Binary Search approach
Using a Spring Cloud App Starter
Checking for Empty or Blank Strings in Java
Tips for dealing with HTTP-related problems
Exception Handling in Java
Từ khóa static và final trong java
HttpClient with SSL
Java – Reader to String