21

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 2

33

You can do

byte[] bytes = stringWriter.toString().getBytes("UTF-8");
Sign up to request clarification or add additional context in comments.

Comments

17

You need to decide which encoding you want to use:

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.