java开发时在以流的方式读取文件内容时,往往会中文乱码,那么就要考虑到统一编码格式
InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);
获得文件编码
/**
* 获得文件编码
* @param fileName
* @return
* @throws Exception
*/
public static String codeString(String fileName) throws Exception {
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
int p = (bin.read() << 8) + bin.read();
bin.close();
String code = null;
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
return code;
}