Table of Contents
In this quick tutorial we’re going to convert a Reader into a String using plain Java, Guava and the Apache Commons IO library.
This article is part of the “Java – Back to Basic” series here on VietMX’s Blog.
1. With Java
Let’s start with a simple Java solution that reads characters sequentially from the Reader:
@Test
public void givenUsingPlainJava_whenConvertingReaderIntoStringV1_thenCorrect()
throws IOException {
StringReader reader = new StringReader("text");
int intValueOfChar;
String targetString = "";
while ((intValueOfChar = reader.read()) != -1) {
targetString += (char) intValueOfChar;
}
reader.close();
}
If there is a lot of content to read, a bulk-read solution will work better:
@Test
public void givenUsingPlainJava_whenConvertingReaderIntoStringV2_thenCorrect()
throws IOException {
Reader initialReader = new StringReader("text");
char[] arr = new char[8 * 1024];
StringBuilder buffer = new StringBuilder();
int numCharsRead;
while ((numCharsRead = initialReader.read(arr, 0, arr.length)) != -1) {
buffer.append(arr, 0, numCharsRead);
}
initialReader.close();
String targetString = buffer.toString();
}
2. With Guava
Guava provides a utility that can do the conversion directly:
@Test
public void givenUsingGuava_whenConvertingReaderIntoString_thenCorrect()
throws IOException {
Reader initialReader = CharSource.wrap("With Google Guava").openStream();
String targetString = CharStreams.toString(initialReader);
initialReader.close();
}
3. With Commons IO
Same with Apache Commons IO – there is an IO utility capable of performing the direct conversion:
@Test
public void givenUsingCommonsIO_whenConvertingReaderIntoString_thenCorrect()
throws IOException {
Reader initialReader = new StringReader("With Apache Commons");
String targetString = IOUtils.toString(initialReader);
initialReader.close();
}
And there you have it – 4 ways of transforming a Reader into a plain String. Make sure to check out the sample over on GitHub.
Related posts:
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Python String format_map()
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
The Difference Between Collection.stream().forEach() and Collection.forEach()
A Guide to WatchService in Java NIO2
RestTemplate Post Request with JSON
Java Program to Implement RenderingHints API
Hashtable trong java
Spring Boot Actuator
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Request a Delivery / Read Receipt in Javamail
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Lớp lồng nhau trong java (Java inner class)
Java Program to Describe the Representation of Graph using Incidence List
String Initialization in Java
Java Program to Implement Ternary Search Tree
Mapping a Dynamic JSON Object with Jackson
Spring MVC + Thymeleaf 3.0: New Features
Spring Boot - Servlet Filter
Java Program to Implement Jarvis Algorithm
A Guide to EnumMap
Inject Parameters into JUnit Jupiter Unit Tests
Spring Security and OpenID Connect
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Hướng dẫn Java Design Pattern – Visitor
How to Store Duplicate Keys in a Map in Java?
Biểu thức Lambda trong Java 8 – Lambda Expressions
Python String capitalize()
The StackOverflowError in Java
Integer Constant Pool trong Java
How to use the Spring FactoryBean?