Program to Convert Byte Array to Writer in Java


Byte Array can be said as the combination of byte and array. Byte is the smallest integer type and Array is a linear data structure that is used to store groups of elements of similar datatypes. This article will discuss a program to convert Byte Array to Writer in Java. Let’s understand the term Byte Array and Writer first.

Byte Array and Writer Class

Byte Array

It is a type of array that stores a sequence of data of byte datatype. We will use the ‘getBytes()’ method to convert specified strings into sequences of characters and store them in a byte array.

Syntax

byte[] nameOfarray; 
// declaration
Or,
byte nameOfarray[]; 
// declaration
Or,
   // declaration with size
byte nameOfarray[] = new byte[sizeofarray]; 
   // declaration and initialization
byte nameOfarray[] = {values separated with comma};

We can use any of the above syntaxes in our program.

Writer Class

It is a class that represents character streams. In this article, we will use one of its sub class named ‘StringWriter’ that is used to obtain character streams of string. Also, to insert the elements into the object of Writer class, we will use its inbuilt method ‘append()’.

Syntax

Writer nameOfObject = new StringWriter();

Program to convert Byte Array to Writer

Example 1

The following example illustrates the conversion of byte array to writer class.

Approach

  • Define a parameterized user defined method named ‘printWr()’ to convert byte array to the writer.

  • Inside this method, create an object of ‘Writer’ class and then, in the try block insert elements into writer class object using ‘append()’ method.

  • Moving aheads return the values stored in the object using ‘toString()’ method. This method converts the value of object into string form.

  • Now, inside main() method, declare and initialize two variables and convert them to byte arrays using ‘getBytes()’ method.

  • In the end, call the ‘printWr()’ method along with byte arrays as arguments.

import java.io.*;
public class ByteWr {
   // method to convert byte array to writer
   String printWr(byte[] data1, byte[] data2) {
      Writer newWr = new StringWriter(); 
      // object of writer class
      try { 
         // appending data to obejct of writer class
         newWr.append(new String(data1) );
         newWr.append(new String(data2) );
         newWr.close();
      } catch (Exception exp) {
         // to handle exception
         System.out.println(exp);
      }
      // returning values of object in the form of string
      return newWr.toString(); 
   }
   public static void main(String args[]) {
      ByteWr wrt = new ByteWr(); 
      // create object of ByteWr class
      String st = "Tutorial";
      char chs = 's';
      // storing values into byte array 
      byte data1[] = st.getBytes();
      byte data2[] = Character.toString(chs).getBytes();
      // calling method to pass byte array to writer 
      System.out.println("Byte Array using Writer: " + wrt.printWr(data1, data2) );
   }
}

Output

Byte Array using Writer: Tutorials

Example 2

This is another example that demonstrates the conversion of byte array to writer class.

Approach

  • Declare and initialize two byte arrays.

  • Define an object of the ‘Writer’ class.

  • Inside the try block, insert both byte arrays into the object of ‘Writer’ class using ‘append()’ method.

  • Now, call the object inside ‘println()’ method using toString.

import java.io.*;
public class ByteWr {
   public static void main(String[] args) {
      // declare two byte array
      byte info1[] = "Tutorials".getBytes();
      byte info2[] = "Point".getBytes();
      // define object of writer class
      Writer newWr = new StringWriter(); 
      try {
         // to add data to writer object
         newWr.append(new String(info1));
         newWr.append(new String(info2));
         newWr.close();
      } catch (IOException exp) {  
         // to handle exception
         System.out.println(exp);
      }
      // calling object of writer class
      System.out.println("Byte Array using Writer: " + newWr.toString());
   }
} 

Output

Byte Array using Writer: TutorialsPoint

Conclusion

We started this article by defining the byte array and its syntax of declaration. Then, we understood the basic concept of writer class and in the end, we discussed two examples of Java programs that converts byte array to writer.

Updated on: 15-May-2023

161 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements