字符集真是个恼人的问题,明明System.out.println(System.getProperty("file.encoding"));的结果是UTF-8,但是如果InputStreamReader reader = new InputStreamReader(is, "UTF-8")就是乱码,只有InputStreamReader reader = new InputStreamReader(is, "GB2312")或者InputStreamReader reader = new InputStreamReader(is, "GBK")才正常。这到底是怎么回事呢,百度了也没看懂,谁给我解释下呀
public static void main(String[] args){ String dir = "C:/Users/tao/config/nvshen-sensitive-words.properties"; File file = new File(dir); InputStream is = null; try { is = new FileInputStream(file); System.out.println(System.getProperty("file.encoding")); InputStreamReader reader = new InputStreamReader(is, "GB2312"); Properties prop = new Properties(); prop.load(reader); Enumeration<String> en = (Enumeration<String>) prop.propertyNames(); while (en.hasMoreElements()) { String word = en.nextElement(); String[] strarr = prop.getProperty(word).split("。"); for(String str:strarr){ if("共产党".contains(str)){ System.out.println("contain"); } } } return; } catch (UnsupportedEncodingException e) { System.out.println("not contain"); } catch (IOException e) { System.out.println("not contain"); } finally { if (is != null) try { is.close(); } catch (IOException e) { } } System.out.println("not contain"); }