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:

Spring Boot Configuration with Jasypt
Lớp lồng nhau trong java (Java inner class)
Java Program to Implement Pairing Heap
Java Program to Implement Expression Tree
Java Program to Implement Graph Coloring Algorithm
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Implement Skip List
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Hướng dẫn Java Design Pattern – Intercepting Filter
Java Program to Perform Finite State Automaton based Search
Java List UnsupportedOperationException
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Spring AMQP in Reactive Applications
RestTemplate Post Request with JSON
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Java Program to Implement Sieve Of Sundaram
Compare Two JSON Objects with Jackson
Python String ljust()
Quick Intro to Spring Cloud Configuration
Setting Up Swagger 2 with a Spring REST API
An Intro to Spring Cloud Security
Jackson Annotation Examples
Java Program to Implement Cubic convergence 1/pi Algorithm
Giới thiệu Google Guice – Binding
Limiting Query Results with JPA and Spring Data JPA
CharSequence vs. String in Java
So sánh HashSet, LinkedHashSet và TreeSet trong Java
String Set Queries
Object Type Casting in Java
Spring’s RequestBody and ResponseBody Annotations
Spring WebClient Filters
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5