Java – String to Reader

In this quick tutorial we’ll take a look at how to convert a String to a Reader ,first using plain Java then Guava and finally the Commons IO library.

This article is part of the “Java – Back to Basic” series here on VietMX’s Blog.

1. With Plain Java

Let’s start with the Java solution:

@Test
public void givenUsingPlainJava_whenConvertingStringIntoReader_thenCorrect() throws IOException {
    String initialString = "With Plain Java";
    Reader targetReader = new StringReader(initialString);
    targetReader.close();
}

As you can see, the StringReader is available out of the box for this simple conversion.

2. With Guava

Next – the Guava solution:

@Test
public void givenUsingGuava_whenConvertingStringIntoReader_thenCorrect() throws IOException {
    String initialString = "With Google Guava";
    Reader targetReader = CharSource.wrap(initialString).openStream();
    targetReader.close();
}

We’re making use here of the versatile CharSource abstraction that allows us to open up a Reader from it.

3. With Apache Commons IO

And finally – here’s the Commons IO solution, also using a ready to go Reader implementation:

@Test
public void givenUsingCommonsIO_whenConvertingStringIntoReader_thenCorrect() throws IOException {
    String initialString = "With Apache Commons IO";
    Reader targetReader = new CharSequenceReader(initialString);
    targetReader.close();
}

So there we have it – 3 dead simple ways to convert a String to a Reader in Java. Make sure to check out the sample over on GitHub.

Related posts:

Debug a JavaMail Program
Python String swapcase()
Map Serialization and Deserialization with Jackson
Spring Boot Tutorial – Bootstrap a Simple Application
Hướng dẫn Java Design Pattern – Singleton
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
How to Break from Java Stream forEach
Python String partition()
Java Program to Implement Counting Sort
Python String count()
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
The StackOverflowError in Java
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Marker Interface trong Java
Reading an HTTP Response Body as a String in Java
Python String startswith()
Apache Commons Collections SetUtils
Spring Boot Configuration with Jasypt
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Describe the Representation of Graph using Incidence List
Java Program to Find Nearest Neighbor Using Linear Search
Giới thiệu Google Guice – Dependency injection (DI) framework
Lớp Arrarys trong Java (Arrays Utility Class)
Java Program to Create a Random Graph Using Random Edge Generation
Logout in an OAuth Secured Application
Java Program to Implement Park-Miller Random Number Generation Algorithm
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Spring Data MongoDB Transactions
Auditing with JPA, Hibernate, and Spring Data JPA
Handling URL Encoded Form Data in Spring REST
Java Program to Check whether Undirected Graph is Connected using BFS