首页 > Windows开发 > 详细

C#中FileStream

时间:2018-04-12 17:41:52      阅读:261      评论:0      收藏:0      [点我收藏+]

1.写文件

FileStream fsWrite = new FileStream(@"E:/新建文件夹/OriginalData.dat", FileMode.OpenOrCreate, FileAccess.Write);

string msg = "字符串字符串字符串";

 byte[] myByte = System.Text.Encoding.UTF8.GetBytes(msg);

fsWrite.Write(myByte , 0, myByte .Length);

2.读文件

 1          StringBuilder sb = new StringBuilder();
 2             //读取数据文件
 3             using (FileStream fsRead = new FileStream(@"E:/新建文件夹/OriginalData.dat", FileMode.OpenOrCreate, FileAccess.Read))
 4             {
 5                 long otherLength = fsRead.Length;//还没读取的文件长度
 6                 byte[] buffer = new byte[128];//接收文件的字节数组
 7                 int num = 0;//实际读取的字节数
 8                 int readStartPosit = 0;//开始读取的位置
 9                 while (readStartPosit < 256)
10                 {
11                     fsRead.Position = readStartPosit;//设置文件读取的位置
12                     if (otherLength < buffer.Length)//当剩余文件小于最大读取时
13                     {
14                       num=  fsRead.Read(buffer, readStartPosit, Convert.ToInt32(otherLength));
15                     }
16                     else
17                     {
18                       num=  fsRead.Read(buffer, 0, buffer.Length);
19                     }
20                     if (num <= 0) break;
21 
22                     otherLength -= num;
23                     readStartPosit += num;
24                     sb.Append(Encoding.UTF8.GetString(buffer));
25                 }
26             }
27             Txt1 = sb.ToString();

 

C#中FileStream

原文:https://www.cnblogs.com/LY-HeroesRebor/p/8809004.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!