C#.NET文件流的输入和输出

来源:互联网 发布:极客大数据 编辑:程序博客网 时间:2024/06/15 18:10

一、System.IO常用类

   System.IO 命名空间有各种不同的类,用于执行各种文件操作,如创建和删除文件、读取或写入文件,关闭文件等。

  • BinaryReader                     从二进制流读取原始数据。
  • BinaryWriter                      以 二进制格式写入原始数据。
  • BufferedStream                 字节流的临时存储。
  • Directory                            有助于操作目录结构。
  • DirectoryInfo                      用于对目录执行操作。
  • DriveInfo                            提供驱动器的信息。
  • File                                     有助于处理文件。
  • FileInfo                               用于对文件执行操作。
  • FileStream                          用于文件中任何位置的读写。
  • MemoryStream                  用于随机访问存储在内存中的数据流。
  • Path                                    对路径信息执行操作。
  • StreamReader                    用于从字节流中读取字符。
  • StreamWriter                     用于向一个流中写入字符。
  • StringReader                      用于读取字符串缓冲区。
  • StringWriter                       用于写入字符串缓冲区。

二、LOG文件的打印输出实现方法:
         string path = @"E:\test.txt";//指定LOG输出路径,固定路径        //Path.GetDirectoryName(?)//也可以使用path类来获取动态路径        private void initLogFile()        {            if (File.Exists(path))                File.Delete(path);            //StreamWriter Logfile = File.AppendText(path);//utf-8格式创建一个文件            StreamWriter Logfile = File.CreateText(path);            //以上两种都是可以的            Logfile.Close();//关闭这个线程        }        public void PrintLog(string LogStream)        {                  //StreamWriter Logfile = File.AppendText(path);            StreamWriter Logfile = new StreamWriter(path, true);            //以上两种方式都可以使用            Logfile.WriteLine(LogStream);            Logfile.Flush();            Logfile.Close();        }

三、data文件时时更新与读取实现方法:


  文件的逐行读取方以及显示

        private void ReadFile_Click(object sender, EventArgs e)        {            StreamReader sr = new StreamReader(path, Encoding.UTF8);            String line;            while ((line = sr.ReadLine()) != null)            {                richTextBox1.Text += line;                richTextBox1.Text += "\n";            }            sr.Close();        }



四、windows File dialog使用技巧







0 0
原创粉丝点击