Java examples for Internationalization:Charset
String to UTF-8
//package com.java2s; import java.io.UnsupportedEncodingException; public class Main { public static void main(String[] argv) throws Exception { String str = "java2s.com"; System.out.println(toUTF_8(str)); }// www . j ava2 s. c om public static final String UTF_8 = "UTF-8"; public static String toUTF_8(String str) throws UnsupportedEncodingException { return changeCharset(str, UTF_8); } public static String changeCharset(String str, String newCharset) throws UnsupportedEncodingException { if (str != null) { // ?windows?GB2312 byte[] bs = str.getBytes(); return new String(bs, newCharset); // } return null; } public static String changeCharset(String str, String oldCharset, String newCharset) throws UnsupportedEncodingException { if (str != null) { // byte[] bs = str.getBytes(oldCharset); return new String(bs, newCharset); } return null; } }