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:
Spring Boot - Application Properties
Java Program to Perform Cryptography Using Transposition Technique
Java Program to implement Array Deque
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
New Features in Java 15
Lập trình đa luồng với CompletableFuture trong Java 8
Python String zfill()
A Custom Media Type for a Spring REST API
Introduction to Spring Data MongoDB
String Operations with Java Streams
Introduction to the Java NIO2 File API
JavaScript Methods of RegExp and String
String Set Queries
Using Spring @ResponseStatus to Set HTTP Status Code
Guide to the Synchronized Keyword in Java
An Intro to Spring Cloud Task
Chuyển đổi từ HashMap sang ArrayList
Enum trong java
Spring Boot - Eureka Server
How to Read a File in Java
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Hướng dẫn Java Design Pattern – Command
Guide to Apache Commons CircularFifoQueue
How to Manually Authenticate User with Spring Security
Quick Intro to Spring Cloud Configuration
A Quick Guide to Spring MVC Matrix Variables
Java Program to implement Associate Array
Java Program to Implement Radix Sort
Dynamic Proxies in Java
Java Program to implement Priority Queue
Assert an Exception is Thrown in JUnit 4 and 5