得抛异常
import java.io.UnsupportedEncodingException;
import java.util.*;
class EncodeDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
String s="你好";
byte[] b=s.getBytes("GBK");
System.out.println(Arrays.toString(b));
String s1=new String(b,"SO8859-1");
System.out.println("s1="+s1);
}
}
或者try catch 处理
import java.io.UnsupportedEncodingException;
import java.util.*;
class EncodeDemo {
public static void main(String[] args) {
String s="你好";
byte[] b = null;
try {
b = s.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Arrays.toString(b));
String s1 = null;
try {
s1 = new String(b,"SO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("s1="+s1);
}
}
|