/**
* 判别TXT文档的编码方式
*/
public static String getTxtType(File file) throws IOException {
// TODO Auto-generated method stub
InputStream inputStream=new FileInputStream(file);
byte []head=new byte[3];
inputStream.read(head);
String code="";
code="gb2312";
if(head[0]==-1&&head[2]==-2){
code="UTF-16";
}
if(head[0]==-2&&head[2]==-1){
code="Unicode";
}
if(head[0]==-17&&head[2]==-69){
code="UTF-8";
}
return code;
}
* 判别TXT文档的编码方式
*/
public static String getTxtType(File file) throws IOException {
// TODO Auto-generated method stub
InputStream inputStream=new FileInputStream(file);
byte []head=new byte[3];
inputStream.read(head);
String code="";
code="gb2312";
if(head[0]==-1&&head[2]==-2){
code="UTF-16";
}
if(head[0]==-2&&head[2]==-1){
code="Unicode";
}
if(head[0]==-17&&head[2]==-69){
code="UTF-8";
}
return code;
}
本文介绍了一种简单的方法来判断TXT文档的编码类型,包括GB2312、UTF-16、Unicode和UTF-8等常见编码。通过读取文件头部几个字节并根据特定的字节特征进行判断。
2522

被折叠的 条评论
为什么被折叠?



