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 Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
A Custom Media Type for a Spring REST API
Java Program to Implement ScapeGoat Tree
Java Program to Find the GCD and LCM of two Numbers
Hướng dẫn Java Design Pattern – Dependency Injection
Java Map With Case-Insensitive Keys
Java Program to Implement PriorityBlockingQueue API
Default Password Encoder in Spring Security 5
Java Program to Implement ArrayDeque API
Handle EML file with JavaMail
New Features in Java 13
Template Engines for Spring
New in Spring Security OAuth2 – Verify Claims
Introduction to Project Reactor Bus
Java Program to Implement TreeSet API
Python String capitalize()
Java – Rename or Move a File
Spring Boot - Application Properties
Java Program to Use rand and srand Functions
Java Byte Array to InputStream
Java Program to Implement the One Time Pad Algorithm
How to Use if/else Logic in Java 8 Streams
HashMap trong Java hoạt động như thế nào?
Converting between an Array and a List in Java
Mệnh đề Switch-case trong java
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Introduction to the Java NIO2 File API
SOAP Web service: Authentication trong JAX-WS
Guide to the Java TransferQueue
Comparing Strings in Java
Stack Memory and Heap Space in Java
Collect a Java Stream to an Immutable Collection