In this article, we are going to present various ways to convert Reader to Byte Array in Java. The solutions presented here will be based on pure Java and using libraries such as
Apache Commons IO.
If you are looking for more Java I/O related articles, please check the following links:
2. Convert Reader to Byte Array using
Let's start with the solution in plain Java. The first part of the conversion is actually the Reader to String transformation.
In this example first we covert
String and then we get bytes from that
3. Reader to a byte array using
Guava library also can we used to convert Reader to byte
In this example, we used
Guava utility class
CharStreams which gets
String from the given
Reader object. Then, we get bytes from that resulted
4. Convert Reader to byte
Apache Commons IO library
Apache Commons IO comes with a dedicated method that converts
Byte Array directly:
In this example we used
IOUtils.toByteArray(..) method. Note that we should always give encoding of the given
String in such transformations.
In this article, we presented several ways to convert Reader to a byte
 using plain Java,
Apache Commons IO library.
Like always, examples used in this article can we found at our GitHub repository.