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.
2 Answers
You need to decide which encoding you want to use:
- Call
StringWriter.toString()to get the contents out as a string - Call
String.getBytes(Charset)orString.getBytes(String)to convert the string to a byte array
Don't use the parameterless String.getBytes() call - that will use the platform default encoding. Even if you want to use that, I'd strongly encourage you to specify it explicitly, to make it clear to anyone who reads it.