码迷,mamicode.com
首页 > Windows程序 > 详细

C#文件操作

时间:2015-06-05 19:30:49      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

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);        
View Code

  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();            
View Code

  3)StreamWrite的使用:

技术分享
             FileStream aFile = new FileStream("Log.txt", FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(aFile);
                sw.WriteLine("now: {0} ",DateTime.Now.ToLongDateString());
                sw.Close();            
View Code

 

C#文件操作

标签:

原文地址:http://www.cnblogs.com/shenchao/p/4555327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!