Convert gb2312 files to unicode format
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace crmtools
{
Class fileutil
{
Public static string readfile (string filename)
{
// Return system. Io. file. readalltext (filename, system. Text. encoding. Default );
System. Text. stringbuilder sb = new stringbuilder ();
System. Io. filestream FS = system. Io. file. Open (filename, system. Io. filemode. Open );
Byte [] B = new byte [1024];
While (FS. Read (B, 0, B. Length)> 0)
{
SB. append (system. Text. encoding. Default. getstring (B ));
}
FS. Close ();
Return sb. tostring ();
}
///
/// Convert a file to utf8 Encoding
///
/// /// ///
Public static bool convertfileencode (string srcfilename, string destencode)
{< br> If (isutf8file (srcfilename) return true;
try
{< br> string destfilename = system. io. path. gettempfilename ();
system. text. stringbuilder sb = new stringbuilder ();
system. io. filestream FS = system. io. file. open (srcfilename, system. io. filemode. open);
/*
System. Io. streamwriter Sw = system. Io. file. createtext (destfilename );
Byte [] B = new byte [1024];
While (FS. Read (B, 0, B. Length)> 0)
{
String line = system. Text. encoding. Default. getstring (B );
Line = gb2312tounicode (line );
Sw. Write (line );
}
Sw. Close ();
FS. Close ();
*/
System. Io. filestream Sw = system. Io. file. Create (destfilename );
If (FS. length> 0)
{
Sw. writebyte (0xff );
Sw. writebyte (0xfe );
}
Byte [] B = new byte [1024];
While (true)
{
Int I = FS. Read (B, 0, B. Length );
Byte [] BC = new byte [I];
Array. Copy (B, BC, I );
If (I <= 0) break;
Byte [] btemp = gb2312tounicodebytes (BC );
Foreach (byte Bi in btemp)
{
Sw. writebyte (BI );
}
}
Sw. Close ();
FS. Close ();
System. Io. file. Copy (destfilename, srcfilename, true );
System. Io. file. Delete (destfilename );
}
Catch
{
Return false;
}
Return true;
}
///
/// Determine whether a file is a UTF-8 File
///
//////
Public static bool isutf8file (string srcfilename)
{
Bool isutf8 = false;
System. Io. filestream FS = system. Io. file. Open (srcfilename,
System. Io. filemode. Open, system. Io. fileaccess. Read, system. Io. fileshare. Read );
Byte [] B = new byte [2];
If (FS. Read (B, 0, B. Length)> 0)
{
If (B [0] = 0xff & B [1] = 0xfe)
{
Isutf8 = true;
}
}
FS. Close ();
Return isutf8;
}
// Public static string gb2312tounicodestring (string content)
//{
// String gb2312info = string. empty;
// Encoding utf8 = encoding. Unicode;
// Encoding gb2312 = encoding. getencoding ("gb2312 ");
/// Convert the string into a byte [].
// Byte [] gb2312bytes = gb2312.getbytes (content );
/// Perform the conversion from one encoding to the other.
// Byte [] utf8bytes = encoding. Convert (gb2312, utf8, gb2312bytes );
/// Convert the new byte [] into a char [] and then into a string.
/// This is a slightly different approach to converting to pair strate
/// The use of getcharcount/getchars.
// Char [] utf8chars = new char [utf8.getcharcount (utf8bytes, 0, utf8bytes. Length)];
// Utf8.getchars (utf8bytes, 0, utf8bytes. length, utf8chars, 0 );
// String utf8info = new string (utf8chars );
// Return utf8info;
//}
Public static byte [] gb2312tounicodebytes (byte [] gb2312bytes)
{
Encoding gb2312 = encoding. getencoding ("gb2312 ");
Encoding utf8 = encoding. Unicode;
Byte [] utf8bytes = encoding. Convert (gb2312, utf8, gb2312bytes );
Return utf8bytes;
}
}
}