博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#文件读取和写入的方式总结
阅读量:7243 次
发布时间:2019-06-29

本文共 1849 字,大约阅读时间需要 6 分钟。

hot3.png

1.使用FIleStream(它存储或者读取都是用BYTE数组或者是BYTE)

      1.1文件写入部分:

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

 

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

 

2.StreamWriter和StreamReader(操作对象是字符或者字符串)

       2.1文件读取部分:

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

 

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://my.oschina.net/u/2260184/blog/518426

你可能感兴趣的文章
MySql双机热备解决方案
查看>>
接受数据的三种方式
查看>>
在线参考
查看>>
关于MyEclipse工程部署不能实时同步到Tomcat问题的解决
查看>>
本地MySQL数据库要访问远程MySQL数据库的表中的数据的实现
查看>>
一次完整的HTTP请求所经历的7个步骤
查看>>
线性代数--矩阵的逆
查看>>
android 内存 转载
查看>>
javascript中encodeURI和decodeURI方法
查看>>
ubuntu中的tomcat使用apr模式
查看>>
在线广告清除助手
查看>>
java单态设计模式
查看>>
全局变量与超级全局变量
查看>>
style="visibility: hidden"和 style=“display:none区别
查看>>
去掉UCenter验证码的修改方法
查看>>
我的码云传送门
查看>>
CSS 文本
查看>>
The data couldn’t be read because it isn’t in the correct format.
查看>>
SVN静态库不能提交问题解决
查看>>
JS树形递归
查看>>