public class DemoEncoding { public static void main(String[] args) throws IOException { String data = "中国"; // 编码 byte[] bytes = data.getBytes("gbk"); // 正确解码 String result1 = new String(bytes, "gbk"); System.out.println("result1="+result1);// --中国 // 错误解码 String result2 = new String(bytes, "utf-8"); System.out.println("result2="+result2);// --?й? //解錯碼 byte[] bytes1 = data.getBytes(); // 用正确的码表解码 String result3 = new String(bytes1, "utf-8"); System.out.println("result3="+result3);// --中国 //编错了码 失败彻底 byte[] bytes2 = data.getBytes("iso-8859-1"); System.out.println(bytes2[1]);//63 String result4 = new String(bytes2, "gbk"); System.out.println("result4="+result4);//?? // String path = DemoEncoding.class.getClassLoader().getResource("a.txt").getPath(); //FileReader默认使用gbk FileReader fileReader = new FileReader(path); char[] buffer = new char[10]; int len = fileReader.read(buffer); System.out.println(new String(buffer,0,len));//中国 Reader reader = new InputStreamReader(new FileInputStream(path),"utf-8"); char[] buff = new char[10]; int len1 = reader.read(buff); System.out.println(new String(buff,0,len1));//中国 //结论 全部出现 ????? 即 63 表示编错码 //出现 其他怪异中文是解错码 } }