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:

Transactions with Spring and JPA
Java Program to Find Nearest Neighbor Using Linear Search
Phương thức tham chiếu trong Java 8 – Method References
Introduction to Java Serialization
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Beans and Dependency Injection
Java Program to Implement Stack using Linked List
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Lớp lồng nhau trong java (Java inner class)
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Summing Numbers with Java Streams
Spring Security Remember Me
Request a Delivery / Read Receipt in Javamail
Working With Maps Using Streams
Serverless Functions with Spring Cloud Function
Python String isidentifier()
Java Program to Implement Sparse Array
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Java Program to Implement Shoelace Algorithm
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Java InputStream to Byte Array and ByteBuffer
Java Program to Implement Double Order Traversal of a Binary Tree
Filtering a Stream of Optionals in Java
A Guide to ConcurrentMap
Supplier trong Java 8
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Spring Data Java 8 Support
Collection trong java
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Debugging Reactive Streams in Java
Adding Shutdown Hooks for JVM Applications