Java 7 and above
You can also pass your desired encoding to the String
constructor as a Charset
constant from StandardCharsets. This may be safer than passing the encoding as a String
, as suggested in the other answers.
For example, for UTF-8 encoding
String bytesAsString = new String(bytes, StandardCharsets.UTF_8);