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:
Prevent Cross-Site Scripting (XSS) in a Spring Application
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Inheritance with Jackson
Java Program to Find All Pairs Shortest Path
Redirect to Different Pages after Login with Spring Security
Java Program to Implement Hash Tables Chaining with Binary Trees
Spring Security Basic Authentication
Java Program to Implement Miller Rabin Primality Test Algorithm
Python String encode()
Weak References in Java
Python String join()
Guide to Java 8 groupingBy Collector
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Lớp Properties trong java
The Difference Between Collection.stream().forEach() and Collection.forEach()
Python String islower()
Quick Guide to the Java StringTokenizer
Adding Shutdown Hooks for JVM Applications
Getting Started with Custom Deserialization in Jackson
Java Program to Implement Shoelace Algorithm
Spring Cloud – Bootstrapping
Java Program to Implement Binary Search Tree
The Modulo Operator in Java
JUnit 5 @Test Annotation
Check If a String Is Numeric in Java
Java Program to Implement Threaded Binary Tree
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Guide to the Volatile Keyword in Java
Java Program to Implement Strassen Algorithm
Finding the Differences Between Two Lists in Java
Java Program to Implement Meldable Heap