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

 

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of