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:
Xây dựng ứng dụng Client-Server với Socket trong Java
Java Program to Implement Repeated Squaring Algorithm
How to Kill a Java Thread
Java Program to Implement Sorted List
Spring Data JPA @Query
Java Program to Create a Random Linear Extension for a DAG
Object cloning trong java
Convert char to String in Java
Java Stream Filter with Lambda Expression
Spring @Primary Annotation
Netflix Archaius with Various Database Configurations
Lớp Properties trong java
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Java Program to Implement Find all Back Edges in a Graph
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Java Program to Implement Gaussian Elimination Algorithm
Python Program to Capitalize the First Character of a String
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Java – String to Reader
Anonymous Classes in Java
Java Program to Implement LinkedHashMap API
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Implement Maximum Length Chain of Pairs
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java NIO2 Path API
Java Program to Check whether Graph is a Bipartite using BFS
Show Hibernate/JPA SQL Statements from Spring Boot
Java Program to Implement Merge Sort Algorithm on Linked List
A Guide to HashSet in Java
Assert an Exception is Thrown in JUnit 4 and 5
Spring Boot - Batch Service
Guide to Dynamic Tests in Junit 5