Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert java.io.StringWriter to byte[] array

Tags:

java

How to convert java.io.StringWriter to byte[] array? Using OpenCSV i get StringWriter sw object and this sw object I want to convert into byte[] array.

like image 919
Adeeb Cheulkar Avatar asked Nov 19 '12 11:11

Adeeb Cheulkar


People also ask

How do you assign a byte array in Java?

Arrays. fill(). This method assigns the required byte value to the byte array in Java.

How do I print a byte array?

You can simply iterate the byte array and print the byte using System. out. println() method.

Can we convert int to byte in Java?

The byteValue() method of Integer class of java. lang package converts the given Integer into a byte after a narrowing primitive conversion and returns it (value of integer object as a byte).


1 Answers

You can do

byte[] bytes = stringWriter.toString().getBytes("UTF-8");
like image 187
Peter Lawrey Avatar answered Sep 28 '22 08:09

Peter Lawrey