import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
/**
* describe:和文件相关的操作类
*
* @author liuli
* @date 2019/02/25
*/
public class MyFileUtil {
//判断编码格式方法
public static String getFilecharset(File sourceFile) {
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1) {
return charset; //文件编码为 ANSI
} else if (first3Bytes[0] == (byte) 0xFF
&& first3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE"; //文件编码为 Unicode
checked = true;
} else if (first3Bytes[0] ==
判断TXT文件编码方式
最新推荐文章于 2024-07-06 14:58:04 发布
本文介绍如何使用Java来判断TXT文件的编码方式,提供了一个调用事例,帮助正确解析文件。
摘要由CSDN通过智能技术生成