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

简介: 1.使用FIleStream(它存储或者读取都是用BYTE数组或者是BYTE)       1.1文件写入部分:                                     string path = "C:\\test.


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

目录
相关文章
|
3月前
|
存储 C语言
C 语言文件读取全指南:打开、读取、逐行输出
要从文件读取,可以使用 r 模式: FILE *fptr; // 以读取模式打开文件 fptr = fopen("filename.txt", "r"); 这将使 filename.txt 打开以进行读取。 在 C 中读取文件需要一点工作。坚持住!我们将一步一步地指导您。 接下来,我们需要创建一个足够大的字符串来存储文件的内容。 例如,让我们创建一个可以存储多达 100 个字符的字符串:
189 2
C 语言文件读取全指南:打开、读取、逐行输出
|
22天前
|
C语言 C++
C/C++文件读取操作
C/C++文件读取操作
|
3月前
|
容器
这个错误是因为在读取文件时,管道已经结束
【1月更文挑战第14天】【1月更文挑战第67篇】这个错误是因为在读取文件时,管道已经结束
44 4
|
4月前
|
XML C# 数据格式
C#读取写入文件的三种方式
最近对文件的操作比较频繁。这里记录一下常用的几种文件读写的方式。 我这里使用窗体来做测试,例子在文末,可下载。
52 0
|
Java
I/O流常用复制和读写文件
I/O流常用复制和读写文件
105 0
读取文件的多种方式
读取文件的多种方式和
|
网络协议 测试技术 Go
一次性读取文件 | 学习笔记
快速学习一次性读取文件
77 0
|
开发者 Python Windows
文件的读取方式 | 学习笔记
快速学习 文件的读取方式
75 0
文件的读取方式 | 学习笔记
|
C语言
【C 语言】文件操作 ( 配置文件读写 | 写出或更新配置文件 | 函数形参设置 | 确保打开文件成功 | 统计文件大小 )
【C 语言】文件操作 ( 配置文件读写 | 写出或更新配置文件 | 函数形参设置 | 确保打开文件成功 | 统计文件大小 )
133 0

热门文章

最新文章