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 - Bootstrapping
Annotation trong Java 8
Java Program to Implement Sorted Array
Java IO vs NIO
Java Program to Implement WeakHashMap API
OAuth 2.0 Resource Server With Spring Security 5
Spring Security Login Page with React
Hướng dẫn sử dụng Printing Service trong Java
Java Program to Generate Randomized Sequence of Given Range of Numbers
Java Program to Implement Rope
Spring Data JPA @Modifying Annotation
Java Program to Implement a Binary Search Tree using Linked Lists
Java Program to Implement Queue
Guide to Spring 5 WebFlux
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Java Program to Implement Knight’s Tour Problem
Guide to Java 8 groupingBy Collector
Object cloning trong java
Unsatisfied Dependency in Spring
Java Program to Find the Minimum value of Binary Search Tree
Chuyển đổi giữa các kiểu dữ liệu trong Java
Hướng dẫn Java Design Pattern – Memento
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Java Program to Implement RoleList API
A Guide to BitSet in Java
Life Cycle of a Thread in Java
Object Type Casting in Java
Encode a String to UTF-8 in Java
Java 14 Record Keyword
ClassNotFoundException vs NoClassDefFoundError
Anonymous Classes in Java
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java