Skip to content
Related Articles

Related Articles

Program to convert Byte Array to Writer in Java
  • Last Updated : 11 Dec, 2018

References: Writer Class

Writer class is used to write character stream, by which byte array can be passed as an argument. By this way, byte array can be converted into Writer class. To get the byte array from String, getBytes() method is used.

Below is the implementation of the above approach:


// Java Program Convert
// Byte Array to Writer
public class GFG {
    // Method which convert
    // byte array into Writer Class
    static String writeByte(byte[] byteString,
                            byte[] byteInt,
                            byte[] byteChar,
                            byte[] byteDouble)
        // Declare the writer class
        Writer writer = new StringWriter();
        try {
            // Call append() method
            // to append byte array into
            // writer class as append method
            // takes input of only string object
                .append(new String(byteString)
                        + new String(byteDouble)
                        + new String(byteChar)
                        + new String(byteInt));
        catch (Exception e) {
            System.out.println("Exception: " + e);
        // return the string
        return writer.toString();
    // Driver Code
    public static void main(String args[])
        String str = "Speed of light: ";
        int num = 8;
        char ch = 'e';
        double dec = 3.0;
        // Insert String value
        byte[] byteString = str.getBytes();
        // Insert int value
        byte[] byteInt = Integer.toString(num).getBytes();
        // Insert char value
        byte[] byteChar = Character.toString(ch).getBytes();
        // Insert double value
        byte[] byteDouble = Double.toString(dec).getBytes();
        // Call the method
        System.out.println(writeByte(byteString, byteInt,
                                     byteChar, byteDouble));
Speed of light: 3.0e8

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :