Program to convert Byte Array to Writer in Java
Last Updated :
11 Dec, 2018
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:
import java.io.StringWriter;
import java.io.Writer;
public class GFG {
static String writeByte( byte [] byteString,
byte [] byteInt,
byte [] byteChar,
byte [] byteDouble)
{
Writer writer = new StringWriter();
try {
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 writer.toString();
}
public static void main(String args[])
{
String str = "Speed of light: " ;
int num = 8 ;
char ch = 'e' ;
double dec = 3.0 ;
byte [] byteString = str.getBytes();
byte [] byteInt = Integer.toString(num).getBytes();
byte [] byteChar = Character.toString(ch).getBytes();
byte [] byteDouble = Double.toString(dec).getBytes();
System.out.println(writeByte(byteString, byteInt,
byteChar, byteDouble));
}
}
|
Output:
Speed of light: 3.0e8
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...