天天看点

c#文件读取和写入的方式总结

                                    string path = "C:\\test.txt";

                            if (File.Exists(path))      ///如果文件存在,那么删除文件 

                                     File.Delete(path);

                            FileStream fs = File.Open(path, FileMode.Create);         ///这里第二个表示如果文件不存在,则重新建立一个新的

                            ///FileStream fs = File.Open(path, FileMode.Append);  ///如果要追加内容时用这个

                            fs.Write(bs, 0, bs.Length);                     ///这里的bs是一个数组byte[]

                            fs.Close();

                   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 path="C:\\TEST.txt";

                string st = "";

               if (!File.Exists(path))

                    MessageBox.Show("文件不存在,请先新建一个!");

              else

                  {  

                      byte[] b=new byte[10];

                      StreamReader sr = new StreamReader(path);

                      while ((st = sr.ReadLine()) != null)

                           {

                              tb.AppendText(st);

                             MessageBox.Show(st);

                          }

                     sr.Close();

                 }

             if (File.Exists(path))

                        File.Delete(path);

              StreamWriter sw = new StreamWriter(path,true);  /// true表示文件存在就将内容加在后面,false表示覆盖内容

              sw.Write("today is a good day!");

              sw.Close();

              binaryreader提供了readstring,readbyte,readboolean,readint,readdouble等方法来读取不同类型的数据,而binarywriter则提供write()方法将各种不同类型的值写入当前流(也就是write方法的参数可以是任何基本数据类型)。下面以存储和读取double类型的数据来举例。

                                FileStream fs=File.Create(path1);

                        BinaryWriter bw = new BinaryWriter(fs);

                        bw.Write(100.123);

                        bw.Write(200.524);

                        bw.Close();

                        fs.Close();

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