歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java 判別TXT文檔的編碼方式

Java 判別TXT文檔的編碼方式

日期:2017/3/1 10:04:23   编辑:Linux編程

Java 判別TXT文檔的編碼方式

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;
}

Copyright © Linux教程網 All Rights Reserved