¼ÆËã»úÔÚ×î³õÖ»Ö§³ÖASCII±àÂ룬µ«ÊǺóÀ´ÎªÁËÖ§³ÖÆäËûÓïÑÔÖеÄ×Ö·û£¨±ÈÈ纺×Ö£©ÒÔ¼°Ò»Ð©ÌØÊâ×Ö·û£¨±ÈÈ瀣©£¬¾ÍÒýÈëÁËUnicode×Ö·û¼¯¡£»ùÓÚUnicode×Ö·û¼¯µÄ±àÂ뷽ʽÓкܶ࣬±ÈÈçUTF-7¡¢UTF-8¡¢UnicodeÒÔ¼°UTF-32¡£ÔÚWindows²Ù×÷ϵͳÖУ¬Ò»¸öÎı¾ÎļþµÄÇ°¼¸¸ö×Ö½ÚÊÇÓÃÀ´Ö¸¶¨¸ÃÎļþµÄ±àÂ뷽ʽµÄ¡£
Èç¹ûÄãʹÓÃNotePad»òWordPadÀ´´ò¿ªÒ»¸öÎı¾Îļþ£¬Äã²¢²»Óõ£ÐĸÃÎļþµÄ±àÂ뷽ʽ£¬ÒòΪÕâЩӦÓóÌÐò»áÏȶÁÈ¡ÎļþµÄÇ°¼¸¸ö×Ö½ÚÀ´È·¶¨¸ÃÎļþµÄ±àÂ뷽ʽ£¬È»ºóÓÃÕýÈ·µÄ±àÂ뽫Îı¾ÖеÄÿ¸ö×Ö·ûÏÔʾ³öÀ´¡£ÏÂÃæµÄͼÖУ¬¿ÉÒÔ¿´µ½µ±ÓÃNotePad¼Çʱ¾±£´æÒ»¸öÎĵµÊ±£¬¿ÉÒÔÑ¡ÔñµÄ±àÂ루Encoding£©·½Ê½ÓÐÄÄЩ¡£
ÓÃ.Net¶ÁÈ¡Îı¾Îļþ»òдÈëÎı¾Îļþ£¬Ä㶼²»ÐëÒªµ£ÐıàÂ뷽ʽ¡£.NetÒѾ½«ÕâЩ·â×°ºÃÁË¡£ÔÚ¶ÁÈ¡Ò»¸öÎı¾ÎļþµÄʱºò£¬Èç¹ûÄãÒѾ֪µÀÎı¾Ê¹ÓõÄÊÇʲô±àÂ뷽ʽ£¬Äã¿ÉÒÔÖ¸¶¨Ê¹ÓÃÄÄÖÖ±àÂ뷽ʽ¶ÁÈ¡Îı¾£¬·ñÔòÈç¹û²»Ö¸¶¨±àÂ뷽ʽ£¬.Net»á¶ÁÈ¡Îı¾µÄÇ°¼¸¸ö×Ö½ÚÀ´È·¶¨Ê¹ÓÃÄÄÖÖ±àÂ뷽ʽ¶ÁÈ¡ÎļþÄÚÈݵġ£ÔÚдÈëÎı¾ÎļþµÄʱºò£¬ÄãÒ²¿ÉÒÔÖ¸¶¨ÄãÏëʹÓõıàÂ뷽ʽ¡£Èç¹ûÄãûÓÐÖ¸¶¨±àÂ룬.Net»á¸ù¾ÝдÈëµÄÎı¾ÊÇ·ñº¬ÓÐÌØÊâ×Ö·ûÀ´¾ö¶¨±àÂ뷽ʽ¡£Èç¹ûûÓÐÌØÊâ×Ö·û£¬¾Í²ÉÓÃASCII±àÂ룬Èç¹ûÓÐÌØÊâ×Ö·û£¬¾Í²ÉÓÃUTF-8±àÂë¡£
£¨Ò»£© ¶ÁÈ¡Îļþ
Èç¹ûÄãÒª¶ÁÈ¡µÄÎļþÄÚÈݲ»ÊǺܶ࣬¿ÉÒÔʹÓà File.ReadAllText(FilePath) »òÖ¸¶¨±àÂ뷽ʽ File.ReadAllText(FilePath, Encoding)µÄ·½·¨¡£
ËüÃǶ¼Ò»´Î½«Îı¾ÄÚÈÝÈ«²¿¶ÁÍ꣬²¢·µ»ØÒ»¸ö°üº¬È«²¿Îı¾ÄÚÈݵÄ×Ö·û´®
string str = File.ReadAllText(@"c:\temp\ascii.txt");
// Ò²¿ÉÒÔÖ¸¶¨±àÂ뷽ʽ
string str2 = File.ReadAllText(@"c:\temp\ascii.txt", Encoding.ASCII);
Ò²¿ÉÒÔʹÓ÷½·¨File.ReadAllLines¡£¸Ã·½·¨·µ»ØÒ»¸ö×Ö·û´®Êý×顣ÿһÐж¼ÊÇÒ»¸öÊý×éÔªËØ¡£
string[] strs = File.ReadAllLines(@"c:\temp\ascii.txt");
// Ò²¿ÉÒÔÖ¸¶¨±àÂ뷽ʽ
string[] strs2 = File.ReadAllLines(@"c:\temp\ascii.txt", Encoding.ASCII);
µ±Îı¾µÄÄÚÈݱȽϴóʱ£¬ÎÒÃǾͲ»Òª½«Îı¾ÄÚÈÝÒ»´Î¶ÁÍ꣬¶øÓ¦¸Ã²ÉÓÃÁ÷£¨Stream£©µÄ·½Ê½À´¶ÁÈ¡ÄÚÈÝ¡£.NetΪÎÒÃÇ·â×°ÁËStreamReaderÀà¡£³õʼ»¯StreamReaderÀàÓкܶàÖÖ·½Ê½¡£ÏÂÃæÎÒÂÞÁгö¼¸ÖÖ
StreamReader sr1 = new StreamReader(@"c:\temp\utf-8.txt");
// ͬÑùÒ²¿ÉÒÔÖ¸¶¨±àÂ뷽ʽ
StreamReader sr2 = new StreamReader(@"c:\temp\utf-8.txt", Encoding.UTF8);
FileStream fs = new FileStream(@"C:\temp\utf-8.txt", FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader sr3 = new StreamReader(fs);
StreamReader sr4 = new StreamReader(fs, Encoding.UTF8);
FileInfo myFile = new FileInfo(@"C:\temp\utf-8.txt");
// OpenText ´´½¨Ò»¸öUTF-8 ±àÂëµÄStreamReader¶ÔÏó
StreamReader sr5 = myFile.OpenText();
// OpenText ´´½¨Ò»¸öUTF-8 ±àÂëµÄStreamReader¶ÔÏó
StreamReader sr6 = File.OpenText(@"C:\temp\utf-8.txt");
³õʼ»¯Íê³ÉÖ®ºó£¬Äã¿ÉÒÔÿ´Î¶ÁÒ»ÐУ¬Ò²¿ÉÒÔÿ´Î¶ÁÒ»¸ö×Ö·û £¬»¹¿ÉÒÔÿ´Î¶Á¼¸¸ö×Ö·û£¬ÉõÖÁÒ²¿ÉÒÔÒ»´Î½«ËùÓÐÄÚÈݶÁÍê¡£
// ¶ÁÒ»ÐÐ
string nextLine = sr.ReadLine();
// ¶ÁÒ»¸ö×Ö·û
int nextChar = sr.Read();
// ¶Á100¸ö×Ö·û
int nChars = 100;
char[] charArray = new char[nChars];
int nCharsRead = sr.Read(charArray, 0, nChars);
// È«²¿¶ÁÍê
string restOfStream = sr.ReadToEnd();
ʹÓÃÍêStreamReaderÖ®ºó£¬²»ÒªÍü¼Ç¹Ø±ÕËü£º sr.Closee();
¼ÙÈçÎÒÃÇÐèÒªÒ»ÐÐÒ»ÐеĶÁ£¬½«Õû¸öÎı¾Îļþ¶ÁÍ꣬ÏÂÃæ¿´Ò»¸öÍêÕûµÄÀý×Ó£º
StreamReader sr = File.OpenText(@"C:\temp\ascii.txt");
string nextLine;
while ((nextLine = sr.ReadLine()) != null)
{
Console.WriteLine(nextLine);
}
sr.Close();
£¨¶þ£© дÈëÎļþ
дÎļþºÍ¶ÁÎļþÒ»Ñù£¬Èç¹ûÄãҪдÈëµÄÄÚÈݲ»ÊǺܶ࣬¿ÉÒÔʹÓÃFile.WriteAllText·½·¨À´Ò»´Î½«ÄÚÈÝÈ«²¿Ð´ÈçÎļþ¡£Èç¹ûÄãÒª½«Ò»¸ö×Ö·û´®µÄÄÚÈÝдÈëÎļþ£¬¿ÉÒÔÓÃFile.WriteAllText(FilePath) »òÖ¸¶¨±àÂ뷽ʽ File.WriteAllText(FilePath, Encoding)·½·¨¡£
string str1 = "Good Morning!"; File.WriteAllText(@"c:\temp\test\ascii.txt", str1); // Ò²¿ÉÒÔÖ¸¶¨±àÂ뷽ʽ File.WriteAllText(@"c:\temp\test\ascii-2.txt", str1, Encoding.ASCII);
Èç¹ûÄãÓÐÒ»¸ö×Ö·û´®Êý×飬ÄãÒª½«Ã¿¸ö×Ö·û´®ÔªËض¼Ð´ÈëÎļþÖУ¬¿ÉÒÔÓÃFile.WriteAllLines·½·¨£º
string[] strs = { "Good Morning!", "Good Afternoon!" }; File.WriteAllLines(@"c:\temp\ascii.txt", strs); File.WriteAllLines(@"c:\temp\ascii-2.txt", strs, Encoding.ASCII);
ʹÓÃFile.WriteAllText»òFile.WriteAllLines·½·¨Ê±£¬Èç¹ûÖ¸¶¨µÄÎļþ·¾¶²»´æÔÚ£¬»á´´½¨Ò»¸öÐÂÎļþ£»Èç¹ûÎļþÒѾ´æÔÚ£¬Ôò»á¸²¸ÇÔÎļþ¡£
µ±ÒªÐ´ÈëµÄÄÚÈݱȽ϶àʱ£¬Í¬ÑùҲҪʹÓÃÁ÷£¨Stream£©µÄ·½Ê½Ð´Èë¡£.Net·â×°µÄÀàÊÇStreamWriter¡£³õʼ»¯StreamWriterÀàͬÑùÓкܶ෽ʽ£º
// Èç¹ûÎļþ²»´æÔÚ£¬´´½¨Îļþ£» Èç¹û´æÔÚ£¬¸²¸ÇÎļþ
StreamWriter sw1 = new StreamWriter(@"c:\temp\utf-8.txt");
// Ò²¿ÉÒÔÖ¸¶¨±àÂ뷽ʽ
// true ÊÇ append text, false Ϊ¸²¸ÇÔÎļþ
StreamWriter sw2 = new StreamWriter(@"c:\temp\utf-8.txt", true, Encoding.UTF8);
// FileMode.CreateNew: Èç¹ûÎļþ²»´æÔÚ£¬´´½¨Îļþ£»Èç¹ûÎļþÒѾ´æÔÚ£¬Å׳öÒì³£
FileStream fs = new FileStream(@"C:\temp\utf-8.txt", FileMode.CreateNew, FileAccess.Write, FileShare.Read);
// UTF-8 ΪĬÈϱàÂë
StreamWriter sw3 = new StreamWriter(fs);
StreamWriter sw4 = new StreamWriter(fs, Encoding.UTF8);
// Èç¹ûÎļþ²»´æÔÚ£¬´´½¨Îļþ£» Èç¹û´æÔÚ£¬¸²¸ÇÎļþ
FileInfo myFile = new FileInfo(@"C:\temp\utf-8.txt");
StreamWriter sw5 = myFile.CreateText();
³õʼ»¯Íê³Éºó£¬¿ÉÒÔÓÃStreamWriter¶ÔÏóÒ»´ÎдÈëÒ»ÐУ¬Ò»¸ö×Ö·û£¬Ò»¸ö×Ö·ûÊý×飬ÉõÖÁÒ»¸ö×Ö·ûÊý×éµÄÒ»²¿·Ö¡£
// дһ¸ö×Ö·û
sw.Write('a');
// дһ¸ö×Ö·ûÊý×é
char[] charArray = new char[100];
// initialize these characters
sw.Write(charArray);
// дһ¸ö×Ö·ûÊý×éµÄÒ»²¿·Ö
sw.Write(charArray, 10, 15);
ͬÑù£¬StreamWriter¶ÔÏóʹÓÃÍêºó£¬²»ÒªÍü¼Ç¹Ø±Õ¡£sw.Close(); ×îºóÀ´¿´Ò»¸öÍêÕûµÄʹÓÃStreamWriterÒ»´ÎдÈëÒ»ÐеÄÀý×Ó£º
FileInfo myFile = new FileInfo(@"C:\temp\utf-8.txt");
StreamWriter sw = myFile.CreateText();
string[] strs = { "ÔçÉϺÃ", "ÏÂÎçºÃ" };
foreach (var s in strs)
{
sw.WriteLine(s);
}
sw.Close();