在不知道文件是什么编码的时候,怎么办 ?
这个文件如果是从服务器中直接通过程序得到的,不知道其编码内容,该如何去得到其内容,让内容不是乱码
public
static
String codeString(String fileName)
throws
Exception{
BufferedInputStream bin =
new
BufferedInputStream(
new
FileInputStream(fileName));
int
p = (bin.read() <<
8
) + bin.read();
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;
}