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. Entity Framework 基础操作(1)

    EF是微软推出的官方ORM框架,默认防注入可以配合LINQ一起使用,更方便开发人员. 首先通过SQLSERVER现在有的数据库类生产EF 右键->添加->新建项,选择AOD.NET实体数据 ...

  2. 简单解决 VMWare “无法打开内核设备:\\Global\\vmx86”错误

    在“服务”后右击选择使用管理员打开.然后在一大串服务中找到vm开头的服务项,全部都启动.重新启动vm就ok了(vm需要以管理员身份打开).

  3. C#如何用IL和Emit类通过Calli来实现实例函数与静态函数的调用

    一. 介绍 最近充能看书,在书上看到函数调用可以 " 通过 ldftn 获得函数指针,然后使用 calli 指令 " 来进行调用,并说这种行为 " 类似 C 的函数指针, ...

  4. 关于EFCore线程内唯一

    EntityFramework的线程内唯一 EntityFramework的线程内唯一是通过httpcontext来实现的 public static DbContext DbContext() { ...

  5. 【C#】学习笔记(2)委托Delegate相关

    泛型委托类型,同样是根据杨老师的视频来的. 直接上栗子

  6. 962. Maximum Width Ramp

    本题题意: 在数组中,找到最大的j-i,使得i<j and A[i] <= A[j] 思路: 维持一个递减的栈,遇到比栈顶小的元素,进栈: 比大于等于栈顶的元素-> 找到栈中第一个小 ...

  7. AFNetworking遇到错误 Request failed: unacceptable content-type: text/html

    iOS 使用AFNetworking遇到错误 Request failed: unacceptable content-type: text/html 原因: 不可接受的内容类型 “text/html ...

  8. Android 7.0新特性

    还望支持个人博客站:http://www.enjoytoday.cn 由于google目前不是无法直接在国内访问,故此,对于android 开发平台的7.0新特性做个保存.也可供大家查阅.原文转自an ...

  9. SQL常用增删改查语句--来源于网络

    1.1[插入单行]insert [into] <表名> (列名) values (列值)例:insert into Strdents (姓名,性别,出生日期) values ('开心朋朋' ...

  10. Python—序列化和反序列化模块(json、pickle和shelve)

    什么是序列化 我们把对象(或者变量)从内存中变为可存储或者可传输的过程称为序列化.在python中为pickling,在其他语言中也被称之为serialization,marshalling,flat ...