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:

Java Program to Implement Efficient O(log n) Fibonacci generator
Build a REST API with Spring and Java Config
Case-Insensitive String Matching in Java
Sử dụng CountDownLatch trong Java
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Perform Cryptography Using Transposition Technique
Debugging Reactive Streams in Java
Composition, Aggregation, and Association in Java
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Wrapper Classes in Java
What is Thread-Safety and How to Achieve it?
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Spring Boot - Zuul Proxy Server and Routing
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Hashing a Password in Java
Python String isalnum()
Java String to InputStream
Python Program to Compute all the Permutation of the String
Python Program to Check If a String Is a Number (Float)
Spring Security Custom AuthenticationFailureHandler
Comparing Arrays in Java
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Spring Boot - Exception Handling
Hướng dẫn Java Design Pattern – Template Method
Introduction to PCollections
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Deploy a Spring Boot App to Azure
Java Program to Convert a Decimal Number to Binary Number using Stacks
Serialize Only Fields that meet a Custom Criteria with Jackson
Python String encode()
Connect through a Proxy
Java Program to Implement the String Search Algorithm for Short Text Sizes