Program to convert Byte Array to Writer in Java
References: Writer Class
Approach:
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:
Program:
// Java Program Convert // Byte Array to Writer import java.io.StringWriter; import java.io.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 writer .append(new String(byteString) + new String(byteDouble) + new String(byteChar) + new String(byteInt)); writer.close(); } 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)); } } |
chevron_right
filter_none
Output:
Speed of light: 3.0e8
Recommended Posts:
- Convert byte[] array to File using Java
- Java Guava | Bytes.indexOf(byte[] array, byte target) method with Examples
- Java Guava | Bytes.indexOf(byte[] array, byte[] target) method with Examples
- Program to convert Array to Set in Java
- Program to convert Array to List in Java
- Program to Convert Stream to an Array in Java
- Program to convert Boxed Array to Stream in Java
- Program to convert Primitive Array to Stream in Java
- Java Program to convert Character Array to IntStream
- Program to Convert Set of Integer to Array of Integer in Java
- Writer hashCode() method in Java with Example
- Program to Convert Set to List in Java
- Program to Convert List to Map in Java
- Program to convert a Map to a Stream in Java
- Writer write(int) method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



