Text文档编码识别方法

[url]http://www.cnblogs.com/preacher/p/6084802.html[/url]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace 文章操作工具
{
public class TextHelper
{
public static System.Text.Encoding GetType(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
System.Text.Encoding r = GetType(fs);
fs.Close();
return r;
}

public static System.Text.Encoding GetType(FileStream fs)
{
/*
Unicode
------------------
254

======================
UnicodeBigEndian
-------------------
255

======================
UTF8
-------------------
228
229
230
231
232
233
187

======================
ANSI
-------------------
176
177
179
180
182
185
191
194
196
198
201
202
205
206
208
209
210
211
213
167
213
228
*/
BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
byte[] ss = r.ReadBytes(3);
int lef = ss[0];
int mid = ss[1];
int rig = ss[2];
r.Close();
/* 文件头两个字节是255 254,为Unicode编码;
文件头三个字节 254 255 0,为UTF-16BE编码;
文件头三个字节 239 187 191,为UTF-8编码;*/
if (lef == 255 && mid == 254)
{
return Encoding.Unicode;
}
else if (lef == 254 && mid == 255 && rig == 0)
{
return Encoding.BigEndianUnicode;
}
else if (lef == 254 && mid == 255)
{
return Encoding.BigEndianUnicode;
}
else if (lef == 239 && mid == 187 && rig == 191)
{
return Encoding.UTF8;
}
else if (lef == 239 && mid == 187)
{
return Encoding.UTF8;
}
else if (lef == 196 && mid == 167
|| lef == 206 && mid == 228
|| lef == 202 && mid == 213)
{
return Encoding.Default;
}
else
{
if (lef == 34)
{
if (mid < 220) return Encoding.Default;
else return Encoding.UTF8;
}
else
{
if (lef < 220) return Encoding.Default;
else return Encoding.UTF8;
}
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值