求一种方法来判断文件的encoding....

real3000 2006-10-10 11:33:01
现有N多TXT文件,比如1.txt, 2.txt, 3.txt, 4.txt.....

编码方式为ANSI、UTF-8 或 UTF-16,

我想知道每个文件的编码方式是什么,

现在的方法是记事本打开文件,然后选择“另存为”....

求一种方法,可以判断出文件的encoding,

在线急求,

先谢谢了
...全文
240 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fd7893 2006-10-10
  • 打赏
  • 举报
回复
mark
shalen520 2006-10-10
  • 打赏
  • 举报
回复
mark
ParadiseX 2006-10-10
  • 打赏
  • 举报
回复
大老孟的方法果然不错,要对编码结构比较精通才能做出来呀。
fencole 2006-10-10
  • 打赏
  • 举报
回复
mark,孟老大出山,我只有顶,jf
孟子E章 2006-10-10
  • 打赏
  • 举报
回复
static Encoding GetEncoding(string filepath)
{
Encoding result = null;
FileStream file = null;
try
{
file = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);

if (file.CanSeek)
{
// get the bom, if there is one
byte[] bom = new byte[4];
file.Read(bom, 0, 4);

// utf-8
if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
result = System.Text.Encoding.UTF8;
// ucs-2le, ucs-4le, ucs-16le, utf-16, ucs-2, ucs-4
else if ((bom[0] == 0xff && bom[1] == 0xfe) ||
(bom[0] == 0xfe && bom[1] == 0xff) ||
(bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff))
result = System.Text.Encoding.Unicode;
// else ascii
else
result = System.Text.Encoding.ASCII;
}
else
{
// can't detect, set to default
result = System.Text.Encoding.ASCII;
}

return result;
}
finally
{
if (null != file) file.Close();
}
}

}
bobo0124 2006-10-10
  • 打赏
  • 举报
回复
mark
孟子E章 2006-10-10
  • 打赏
  • 举报
回复
这个可以

static Encoding GetEncoding(string filepath)
{
Encoding result = null;
FileStream file = null;
try
{
file = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);

if (file.CanSeek)
{
// get the bom, if there is one
byte[] bom = new byte[4];
file.Read(bom, 0, 4);

// utf-8
if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
result = System.Text.Encoding.UTF8;
// ucs-2le, ucs-4le, ucs-16le, utf-16, ucs-2, ucs-4
else if ((bom[0] == 0xff && bom[1] == 0xfe) ||
(bom[0] == 0xfe && bom[1] == 0xff) ||
(bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff))
result = System.Text.Encoding.Unicode;
// else ascii
else
result = System.Text.Encoding.ASCII;
}
else
{
// can't detect, set to default
result = System.Text.Encoding.ASCII;
}

return result;
}
finally
{
if (null != file) file.Close();
}
}

}
一杯清茶Kevin 2006-10-10
  • 打赏
  • 举报
回复
The following code example gets the encoding of the specified StreamReader object.

using System;
using System.IO;
using System.Text;

class Test
{

public static void Main()
{
string path = @"c:\temp\MyTest.txt";

try
{
if (File.Exists(path))
{
File.Delete(path);
}

//Use an encoding other than the default (UTF8).
using (StreamWriter sw = new StreamWriter(path, false, new UnicodeEncoding()))
{
sw.WriteLine("This");
sw.WriteLine("is some text");
sw.WriteLine("to test");
sw.WriteLine("Reading");
}

using (StreamReader sr = new StreamReader(path, true))
{
while (sr.Peek() >= 0)
{
Console.Write((char)sr.Read());
}

//Test for the encoding after reading, or at least
//after the first read.
Console.WriteLine("The encoding used was {0}.", sr.CurrentEncoding);
Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
ParadiseX 2006-10-10
  • 打赏
  • 举报
回复
签名, 这个我也关注
real3000 2006-10-10
  • 打赏
  • 举报
回复
CupTea(一杯清茶) 的方法中,reader 的 currentEncoding 总是uft8,也就是默认的encoding...

还是孟子老大的方法比较强;

另外,感谢孟子老大出山,给足了小弟面子^_^

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧