Java - Write code to Convert byte array to String in UTF-8

Requirements

Write code to Convert byte array to String in UTF-8

Demo

//package com.book2s;
import java.io.UnsupportedEncodingException;

public class Main {
    public static void main(String[] argv) {
        byte[] bytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(toUTF8(bytes));
    }/*from   w  w  w  . j ava 2  s  . c  o m*/

    public static String toUTF8(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        try {
            return new String(bytes, "utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e);
        }
    }
}