标签:
1、FileStream只处理原始字节,不处理字符,使用StreamRead和StreamWrite处理字符。
1)使用FileStream处理字符串时,须经过中间类的转换:
byte[] byData = new byte[200]; char[] charData = new Char[200]; //Byte2Char Decoder d = Encoding.UTF8.GetDecoder(); d.GetChars(byData, 0, byData.Length, charData, 0); //Char2Byte Encoder e = Encoding.UTF8.GetEncoder(); e.GetBytes(charData, 0, charData.Length, byData, 0, true);
2)StreamRead的使用:
FileStream aFile = new FileStream("Log.txt", FileMode.Open); StreamReader sr = new StreamReader(aFile); line = sr.ReadLine(); // Read data in line by line. while (line != null) { Console.WriteLine(line); line = sr.ReadLine(); } sr.Close();
3)StreamWrite的使用:
FileStream aFile = new FileStream("Log.txt", FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(aFile); sw.WriteLine("now: {0} ",DateTime.Now.ToLongDateString()); sw.Close();
标签:
原文地址:http://www.cnblogs.com/shenchao/p/4555327.html