您的位置:首页 > 编程语言 > Java开发

Java 判别TXT文档的编码方式

2012-02-14 12:41 295 查看
package com.zzy.code;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

public class GetEncode {

/**

* @param args

* @throws IOException

*/

/*************************************

java编码与txt编码对应

java txt

unicode
unicode big endian

utf-8 utf-8

utf-16
unicode

gb2312
ANSI

***************************************/

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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: