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 Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Spring Boot - Application Properties
@DynamicUpdate with Spring Data JPA
The Order of Tests in JUnit
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Java Program to Implement Horner Algorithm
Add Multiple Items to an Java ArrayList
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Phân biệt JVM, JRE, JDK
Java Program to Implement Shunting Yard Algorithm
Arrays.asList vs new ArrayList(Arrays.asList())
Convert String to Byte Array and Reverse in Java
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Running Spring Boot Applications With Minikube
Java Program to Implement Counting Sort
Removing all Nulls from a List in Java
Removing all Nulls from a List in Java
Java Program to Perform integer Partition for a Specific Case
Java Program to Implement Shell Sort
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Java Program to Implement LinkedHashSet API
Marker Interface trong Java
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Java Program to Implement Fermat Factorization Algorithm
Properties with Spring and Spring Boot
Java InputStream to Byte Array and ByteBuffer
Java Program to Generate Random Hexadecimal Byte
Jackson – JsonMappingException (No serializer found for class)
Java Program to Implement Shoelace Algorithm
Java Program to Solve the 0-1 Knapsack Problem
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Remove All Occurrences of a Specific Value from a List