Convert Byte Array to Writer in Java

In this post, we will discuss how to convert byte array to Writer in Java.


 

1. Naive

The idea is to convert the byte array to a String in Java and appends that String to this writer by using its append() method.

 

Download   Run Code

Output:

Techie Delight

 

2. Guava

We can also use CharSink class provided by Guava library, which is an immutable supplier of Writer instances.

 

Download   Run Code

Output:

Techie Delight

 

3. Apache Commons IO

Apache Commons IO also provides Writer implementation that outputs to a StringBuilder. This implementation offers better performance than java.io.StringWriter as it is not synchronized. The idea is to convert the byte[] to StringBuilderWriter using a StringBuilder.

 

Download

Output:

Techie Delight

 

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of