Over a million developers have joined DZone.

Java Cleanly Decode Utf8

Clean a string of non-utf8 characters in java using nio madness!

    import java.nio.CharBuffer;
    import java.nio.charset.CharacterCodingException;
    import java.nio.charset.CodingErrorAction;
    import java.nio.charset.Charset;
    import java.nio.charset.CharsetDecoder;
    import java.nio.charset.CharsetEncoder;

    public static Charset charset = Charset.forName("UTF-8");
    public static CharsetEncoder encoder = charset.newEncoder();
    public static CharsetDecoder decoder = charset.newDecoder();

    static {

    public static String utf8( String input ) throws CharacterCodingException {
        return decoder.decode( encoder.encode( CharBuffer.wrap( input ) ) ).toString();

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}