c#文件讀取和寫入的方式總結


1.使用FIleStream(它存儲或者讀取都是用BYTE數組或者是BYTE)

      1.1文件寫入部分:

                                    string path = "C:\\test.txt";
                            
                            if (File.Exists(path))      ///若是文件存在,那麼刪除文件 數組

                                     File.Delete(path);spa

                            FileStream fs = File.Open(path, FileMode.Create);         ///這裏第二個表示若是文件不存在,則從新創建一個新的
                            ///FileStream fs = File.Open(path, FileMode.Append);  ///若是要追加內容時用這個對象

                          

                            fs.Write(bs, 0, bs.Length);                     ///這裏的bs是一個數組byte[]
                      
                            fs.Close();字符串

 

     1.2文件讀取部分:

                   string str = "C:\\test.txt";
                        if (!File.Exists(str))     ///檢測文件是否存在
                           {
                                       MessageBox.Show("文件不存在,請查看客戶端是否已經上傳數據!");
                           }
                       else
                          {   
                                 FileStream fop = File.OpenRead(str);
                                 byte[] arr = new byte[1000];
                                 while (fop.Read(arr, 0, arr.Length) > 0)    ///這個循環會將文件全部的內容都讀取出來
                                  {
                                  ClientSocket[1].Send(arr, 0, arr.Length,0);
                                  }
                                 fop.Close();
                             }
string

 

2.StreamWriter和StreamReader(操做對象是字符或者字符串)

       2.1文件讀取部分:

                string path="C:\\TEST.txt";it

                string st = "";
               if (!File.Exists(path))
                    MessageBox.Show("文件不存在,請先新建一個!");
              else
                  {  test

                      byte[] b=new byte[10];
                      StreamReader sr = new StreamReader(path);
                      while ((st = sr.ReadLine()) != null)
                           {
                              tb.AppendText(st);
                             MessageBox.Show(st);
                          }
                     sr.Close();
                
                 }循環

 

       2.2文件寫入部分

             if (File.Exists(path))
                        File.Delete(path);
              StreamWriter sw = new StreamWriter(path,true);  /// true表示文件存在就將內容加在後面,false表示覆蓋內容
              sw.Write("today is a good day!");
              sw.Close();數據類型

 

3.使用binaryreader和binarywriter進行文件讀寫操做(能夠對任何基本數據類型進行操做)

              binaryreader提供了readstring,readbyte,readboolean,readint,readdouble等方法來讀取不一樣類型的數據,而binarywriter則提供write()方法將各類不一樣類型的值寫入當前流(也就是write方法的參數能夠是任何基本數據類型)。下面以存儲和讀取double類型的數據來舉例。方法

     3.1 binarywrite寫入數據

                                FileStream fs=File.Create(path1);
                        BinaryWriter bw = new BinaryWriter(fs);
                        bw.Write(100.123);
                        bw.Write(200.524);
                        bw.Close();
                        fs.Close();

     3.2 binaryreader讀取數據

                               FileStream fs1 = new FileStream(path1, FileMode.Open);                        BinaryReader br = new BinaryReader(fs1);                        Console.WriteLine("{0}",br.ReadDouble());                        Console.WriteLine("{0}", br.ReadDouble());                        br.Close();                        fs.Close();

相關文章
相關標籤/搜索