Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Linq;
Using System.Text;
Using System.Web;
Namespace Wordsearch1
{
public class fileencoding
{
Public fileencoding ()
{
//
TODO: Add constructor logic here
//
}
<summary>
Gets the encoding of a text file. If a valid leader cannot be found at the head of the file, Encoding.default will be returned.
</summary>
<param name= "filename" > file name. </param>
<returns></returns>
public static Encoding GetEncoding (String fileName)
{
Return GetEncoding (FileName, Encoding.default);
}
<summary>
Gets the encoding of a text file stream.
</summary>
<param name= "Stream" > Text file stream. </param>
<returns></returns>
public static Encoding GetEncoding (FileStream stream)
{
Return GetEncoding (stream, Encoding.default);
}
//<summary>
//Gets the encoding of a text file.
//</summary>
//<param name= "filename" > file name. </param>
//<param name= "defaultencoding" > Default encoding method. This encoding is returned when the method cannot get a valid leader from the head of the file. </param>
//<returns></returns>
public static Encoding getencoding (String fileName, Encoding defaultencoding)
{
FileStream fs = new FileStream (FileName, FileMode.Open);
Encoding targetencoding = getencoding (FS, defaultencoding);
FS. Close ();
return targetencoding;
}
<summary>
Gets the encoding of a text file stream.
</summary>
<param name= "Stream" > Text file stream. </param>
<param name= "defaultencoding" > Default encoding method. This encoding is returned when the method cannot get a valid leader from the head of the file. </param>
<returns></returns>
public static Encoding GetEncoding (FileStream stream, Encoding defaultencoding)
{
Encoding targetencoding = defaultencoding;
if (stream! = null && stream. Length >= 2)
{
Save the first 4 bytes of a file stream
byte byte1 = 0;
byte Byte2 = 0;
byte byte3 = 0;
byte byte4 = 0;
Save current seek position
Long Origpos = stream. Seek (0, Seekorigin.begin);
Stream. Seek (0, Seekorigin.begin);
int nbyte = stream. ReadByte ();
Byte1 = Convert.tobyte (nbyte);
Byte2 = Convert.tobyte (stream. ReadByte ());
if (stream. Length >= 3)
{
Byte3 = Convert.tobyte (stream. ReadByte ());
}
if (stream. Length >= 4)
{
Byte4 = Convert.tobyte (stream. ReadByte ());
}
Judging encoding based on the first 4 bytes of the file stream
Unicode {0xFF, 0xFE};
Be-unicode {0xFE, 0xFF};
UTF8 = {0xEF, 0xBB, 0xBF};
if (byte1 = = 0xFE && Byte2 = = 0xFF)//unicodebe
{
targetencoding = Encoding.bigendianunicode;
}
if (byte1 = = 0xFF && Byte2 = = 0xFE && byte3! = 0xFF)//unicode
{
targetencoding = Encoding.unicode;
}
if (byte1 = = 0xEF && Byte2 = = 0xBB && byte3 = 0xBF)//utf8
{
targetencoding = Encoding.UTF8;
}
Restore Seek position
Stream. Seek (Origpos, Seekorigin.begin);
}
return targetencoding;
}
}
}
File character stream encoding judgment