1、FileStream

FileStream 详细介绍参考msdn

写数据:
using (FileStream fs = new FileStream("File.FileStream", FileMode.Create, FileAccess.Write))
{
for (int i = 0; i < Cycles; i++)
{
for (int j = 0; j < Length; j++)
{
dis[j] = i * Length + j;
}
Buffer.BlockCopy(dis, 0, byData, i * readCount, readCount);
}
fs.Write(byData, 0, byData.Length);
}
读数据
using (FileStream fs = new FileStream("File.FileStream", FileMode.Open, FileAccess.Read))
{
for (int i = 0; i < Cycles; i++)
{
fs.Seek(i * readCount, SeekOrigin.Begin);
fs.Read(byData, 0, readCount);
dis = new double[Length];
Buffer.BlockCopy(byData, i * readCount, dis, 0, readCount);
}
}

2、BinaryWriter/BinaryReader

2.1 BinaryWriter(将二进制中的基元类型写入流并支持用特定的编码写入字符串。) 详细介绍参考msdn

using (BinaryWriter bw = new BinaryWriter(File.Open("File.Binary", FileMode.Create)))
{
byte[] data = new byte[Cycles * readCount];
for (int i = 0; i < Cycles; i++)
{
for (int j = 0; j < Length; j++)
{
dis[j] = i * Length + j;
}
Buffer.BlockCopy(dis, 0, data, i * readCount, readCount);
}
bw.Write(data);
}

2.2 BinaryReader (用特定的编码将基元数据类型读作二进制值。)详细介绍参考msdn

using (BinaryReader wr = new BinaryReader(File.Open("File.Binary", FileMode.Open)))
{
for (int i = 0; i < Cycles; i++)
{
var readData = wr.ReadBytes(readCount);
dis = new double[Length];
Buffer.BlockCopy(readData, 0, dis, 0, readCount);
}
}

3、StreamWriter/StreamReader

3.1 StreamWriter 详细介绍参考msdn

 using (StreamWriter sw = new StreamWriter("File.Stream", false, Encoding.GetEncoding("utf-16")))
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Cycles; i++)
{
for (int j = 0; j < Length; j++)
{
dis[j] = i * Length + j;
sb.AppendFormat("{0},", dis[j]);
}
sb.AppendFormat("\n");
}
sw.WriteLine(sb);
}

3.2 StreamReader 详细介绍参考msdn

 using (StreamReader sd = new StreamReader("File.Stream", Encoding.GetEncoding("utf-16")))
{
for (int i = 0; i < Cycles; i++)
{
string[] ch = sd.ReadLine().Split(new Char[] { ',' },
System.StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < Length; j++)
{
double.TryParse(ch[j], out dis[j]);
}
}
}

4 完整测试代码:

class Program
{
static void Main()
{
fileReadAndWrite.BinaryWriterMethod();
fileReadAndWrite.BinaryReaderMethod(); fileReadAndWrite.FileStreamWriterMethod();
fileReadAndWrite.FileStreamReadMethod(); fileReadAndWrite.StreamWriterMethod();
fileReadAndWrite.StreamReaderMethod(); Console.ReadKey(true);
}
}
class FileReadAndWrite
{
private const int Length = 1024;
private const int Cycles = 64;
private int readCount;
private byte[] byData;
private double[] dis; public FileReadAndWrite()
{
readCount = Length * sizeof(double);
dis = new double[Length];
byData = new byte[Cycles * Length * sizeof(double)];
} #region BinaryWriter\BinaryReader
public void BinaryWriterMethod()
{
using (BinaryWriter bw = new BinaryWriter(File.Open("File.Binary", FileMode.Create)))
{
byte[] data = new byte[Cycles * readCount];
for (int i = 0; i < Cycles; i++)
{
for (int j = 0; j < Length; j++)
{
dis[j] = i * Length + j;
}
Buffer.BlockCopy(dis, 0, data, i * readCount, readCount);
}
bw.Write(data);
}
} public void BinaryReaderMethod()
{
using (BinaryReader wr = new BinaryReader(File.Open("File.Binary", FileMode.Open)))
{
for (int i = 0; i < Cycles; i++)
{
var readData = wr.ReadBytes(readCount);
Buffer.BlockCopy(readData, 0, dis, 0, readCount);
}
}
}
#endregion #region FileStream Read\Write
public void FileStreamWriterMethod()
{
using (FileStream fs = new FileStream("File.FileStream", FileMode.Create,FileAccess.Write))
{
for (int i = 0; i < Cycles; i++)
{
for (int j = 0; j < Length; j++)
{
dis[j] = i * Length + j;
}
Buffer.BlockCopy(dis, 0, byData, i * readCount, readCount);
}
fs.Write(byData, 0, byData.Length);
}
} public void FileStreamReadMethod()
{
using (FileStream fs = new FileStream("File.FileStream", FileMode.Open, FileAccess.Read))
{
for (int i = 0; i < Cycles; i++)
{
fs.Seek(i * readCount, SeekOrigin.Begin);
fs.Read(byData, 0, readCount);
Buffer.BlockCopy(byData, i * readCount, dis, 0, readCount);
}
}
}
#endregion #region StreamWriter\StreamReader
public void StreamWriterMethod()
{
using (StreamWriter sw = new StreamWriter("File.Stream", false,
Encoding.GetEncoding("utf-16")))
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Cycles; i++)
{
for (int j = 0; j < Length; j++)
{
dis[j] = i * Length + j;
sb.AppendFormat("{0},", dis[j]);
}
sb.AppendFormat("\n");
}
sw.WriteLine(sb);
}
} public void StreamReaderMethod()
{
using (StreamReader sd = new StreamReader("File.Stream", Encoding.GetEncoding("utf-16")))
{
for (int i = 0; i < Cycles; i++)
{
string[] ch = sd.ReadLine().Split(new Char[] { ',' },
System.StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < Length; j++)
{
double.TryParse(ch[j], out dis[j]);
}
}
}
}
#endregion
}

C# 流介绍 (原发布 csdn 2017-09-15 23:37:52)的更多相关文章

  1. c# Equal函数 and 运算符'==' (原发布 csdn 2017年10月15日 20:39:26)

    1.==.!=.<.>.<= 和>= 运算符为比较运算符(comparison operator).C#语言规范5.0中文版中比较运算符的描述如下: 2.通用类型系统 3.值类 ...

  2. 关于“关于C#装箱的疑问”帖子的个人看法 (原发布csdn 2017年10月07日 10:21:10)

    前言 昨天晚上闲着无事,就上csdn逛了一下,突然发现一个帖子很有意思,就点进去看了一下. 问题很精辟 int a = 1; object b=a; object c = b; c = 2; 为什么b ...

  3. datalab (原发布 csdn 2018年09月21日 20:42:54)

    首先声明datalab本人未完成,有4道题目没有做出来.本文博客记录下自己的解析,以便以后回忆.如果能帮助到你就更好了,如果觉得本文没啥技术含量,也望多多包涵. /* * bitAnd - x& ...

  4. WPF DataGrid显示MySQL查询信息,且可删除、修改、插入 (原发布 csdn 2018-10-13 20:07:28)

    1.入行好几年了,工作中使用数据库几率很小(传统行业).借着十一假期回家机会,学习下数据库. 2.初次了解数据库相关知识,如果本文有误,还望告知. 3.本文主要目的,记录下wpf界面显示数据库信息,且 ...

  5. c# 类实例序列化反序列化json文件 (原发布 csdn 2017-10-01 20:02:12)

    前言 前段时间使用了net.json保存对象数据.添加完成后,测试发现300多实例数据保存加载json文件,速度比原方式(BinaryFormatter)慢.但是功能加上后也懒再删掉代码了,索性就采用 ...

  6. IEEE浮点表示 (原发布 csdn 2018-10-14 10:29:33)

    目录 观察IEEE浮点表示 工作中遇到过整型转浮点型(union那种转换),碰到就看下书,过后就遗忘了.等过段时间又出现此现象,又重新拿起书本,这次记录了过程.然而一直等到今天才写出来,以防以后还用到 ...

  7. WPF 启动页面 (原发布 csdn 2017-06-26 19:26:01)

    如果我写的有误,请及时与我联系,我立即改之以免继续误导他/她人. 如果您有好的想法或者建议,请随时与我联系. wpf软件启动时,加载启动页面.软件初始化完成之后关闭页面. App.xaml.cs代码 ...

  8. wpf 单例模式和异常处理 (原发布 csdn 2017-04-12 20:34:12)

    第一次写博客,如有错误,请大家及时告知,本人立即改之. 如果您有好的想法或者建议,我随时与我联系. 如果发现代码有误导时,请与我联系,我立即改之. 好了不多说,直接贴代码. 一般的错误,使用下面三个就 ...

  9. c# "As" 与 "Is"效率 (原发布csdn 2017-10-07 11:49:18)

    十一长假就要过去了,今年假期没有回家,一个人闲着无聊就在看C#语言规范5.0中文版.昨天看了 is运算符和 as运算符,平时项目中也有用到这两种符号,对于其效率也没有进行比较过,趁着假期有空,先看下效 ...

随机推荐

  1. Z从壹开始前后端分离【 .NETCore2.1 +Vue 2 +AOP+DI】框架之一 || 前言

    老张 .NetCore与Vue 框架学习目录

  2. PHP通过session判断防止表单重复提交实例

    PHP通过session判断防止表单重复提交实例,当用户提交表单后,为防止重复操作,通过session来判断是否为初次提交,否则让他返回到之前表单页面. 当前表单页面is_submit设为0 SESS ...

  3. VS中怎样对C#项目进行单元测试

    场景 SpringBoot+Junit在IDEA中实现查询数据库的单元测试: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/927 ...

  4. Dynamics CRM 客户端程序开发:自定义系统标准按钮的可用性

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复125或者20140414可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 一般是新建一个解决方案用于客制化 ...

  5. 在执行方法和Web资源中获取传递过来参数的值

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复228或者20161026可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

  6. 2_Swift基本数据类型

    数字和基本数据类型 模型数据与数字,布尔值和其他基本类型. 逻辑值 struct Bool 一个值类型实例, 取值true或者flase Bool表示Swift中的布尔值.Bool通过使用其中一个布尔 ...

  7. FastJSON使用例子

    FastjsonTest.java package demo; import java.util.ArrayList; import java.util.Date; import java.util. ...

  8. sqlserver2008R2 本地不能用localhost连接

    问题 在重新安装sql Server2008R2的时候,本地安装完成之后,想用localhost或者127.0.0.1登录的时候发现一直报错,无法连接,以下是解决方案. 打开Sql Server配置管 ...

  9. C语言中的volatile关键字简介

    C语言中的volatile关键字简介: (1)含义:         volatile关键字的意思是可能会被外来的意想不到的改变.它的作用是:优化器在使用该关键字定义的变量时,直接从内存中读取原始的数 ...

  10. 重启电脑 wamp图标是橙色(未变绿)

    记录一个错误: 修复系统漏洞后,重启电脑,wamp没有开机自启动,手动启动后发现,图标是大红色变成了橙色,也就是服务未完全启动(1/2)状态. ??? 但是我其实也不知道是哪个服务(Apache/My ...