C# 流介绍 (原发布 csdn 2017-09-15 23:37:52)
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)的更多相关文章
- c# Equal函数 and 运算符'==' (原发布 csdn 2017年10月15日 20:39:26)
1.==.!=.<.>.<= 和>= 运算符为比较运算符(comparison operator).C#语言规范5.0中文版中比较运算符的描述如下: 2.通用类型系统 3.值类 ...
- 关于“关于C#装箱的疑问”帖子的个人看法 (原发布csdn 2017年10月07日 10:21:10)
前言 昨天晚上闲着无事,就上csdn逛了一下,突然发现一个帖子很有意思,就点进去看了一下. 问题很精辟 int a = 1; object b=a; object c = b; c = 2; 为什么b ...
- datalab (原发布 csdn 2018年09月21日 20:42:54)
首先声明datalab本人未完成,有4道题目没有做出来.本文博客记录下自己的解析,以便以后回忆.如果能帮助到你就更好了,如果觉得本文没啥技术含量,也望多多包涵. /* * bitAnd - x& ...
- WPF DataGrid显示MySQL查询信息,且可删除、修改、插入 (原发布 csdn 2018-10-13 20:07:28)
1.入行好几年了,工作中使用数据库几率很小(传统行业).借着十一假期回家机会,学习下数据库. 2.初次了解数据库相关知识,如果本文有误,还望告知. 3.本文主要目的,记录下wpf界面显示数据库信息,且 ...
- c# 类实例序列化反序列化json文件 (原发布 csdn 2017-10-01 20:02:12)
前言 前段时间使用了net.json保存对象数据.添加完成后,测试发现300多实例数据保存加载json文件,速度比原方式(BinaryFormatter)慢.但是功能加上后也懒再删掉代码了,索性就采用 ...
- IEEE浮点表示 (原发布 csdn 2018-10-14 10:29:33)
目录 观察IEEE浮点表示 工作中遇到过整型转浮点型(union那种转换),碰到就看下书,过后就遗忘了.等过段时间又出现此现象,又重新拿起书本,这次记录了过程.然而一直等到今天才写出来,以防以后还用到 ...
- WPF 启动页面 (原发布 csdn 2017-06-26 19:26:01)
如果我写的有误,请及时与我联系,我立即改之以免继续误导他/她人. 如果您有好的想法或者建议,请随时与我联系. wpf软件启动时,加载启动页面.软件初始化完成之后关闭页面. App.xaml.cs代码 ...
- wpf 单例模式和异常处理 (原发布 csdn 2017-04-12 20:34:12)
第一次写博客,如有错误,请大家及时告知,本人立即改之. 如果您有好的想法或者建议,我随时与我联系. 如果发现代码有误导时,请与我联系,我立即改之. 好了不多说,直接贴代码. 一般的错误,使用下面三个就 ...
- c# "As" 与 "Is"效率 (原发布csdn 2017-10-07 11:49:18)
十一长假就要过去了,今年假期没有回家,一个人闲着无聊就在看C#语言规范5.0中文版.昨天看了 is运算符和 as运算符,平时项目中也有用到这两种符号,对于其效率也没有进行比较过,趁着假期有空,先看下效 ...
随机推荐
- C#反射调用类的私有方法
void Main() { var type = typeof(StockClass); type.Dump(); var fields=type.GetFields(BindingFlags.Ins ...
- java.lang.IllegalArgumentException: java.util.zip.ZipException: invalid LOC header (bad signature)异常解决方法
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start com ...
- JavaScript之找LHS查询和RHS查询
LHS和RHS,当变量出现在赋值操作的左侧时进行LHS 查询,出现在右侧时进行RHS 查询. LHS 查询是试图找到变量的容器本身,从而可以对其赋值. RHS 理解成retrieve his sour ...
- 查找树ADT--二叉查找树
二叉树的一个重要应用是它们在查找中的使用. 二叉查找树的性质:对于树中的每个节点X,它的左子树中所有项的值小于X中的项,而它的右子树中所有项的值大于X中的项.这意味着该树所有的元素可以用某种一致的方式 ...
- HiGV ui代码流程
在海思PDT_Init下有一个这样的函数HI_PDT_UI_Init(): 1. HI_PDT_UI_Init()函数: /*public, it should be called by main() ...
- 2.Java基础_Java常量
/* 常量: 在程序执行过程中,其值不可以发生改变的量 常量分类: 字符串常量: 用双引号括起来的内容. "Hello,World!" 整数常量: 不带小数的数字. 666,-88 ...
- SQL(二)语法
数据库表 一个数据库通常包含一个或多个表.每个表有一个名字标识(例如:"Websites"),表包含带有数据的记录(行). 在本教程中,我们在 MySQL 的 RUNOOB 数据库 ...
- CF1041C Coffee Break
CF1041C Coffee Break 题目大意: 给定nn个数和一个kk,这nn个数都不超过mm 每次从没被去掉的数里面选一个数aa,去掉aa,然后可以任意一个b(b>a+k)b(b> ...
- 剑指offer:序列化二叉(前序遍历+层次)
1. 题目描述 /** 请实现两个函数,分别用来序列化和反序列化二叉树 二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建立起来的二叉树可以持久保存. 序列 ...
- 公式推导【BACF//ICCV2017】
HK Galoogahi, A Fagg, S Lucey. Learning Background-Aware Correlation Filters for Visual Tracking[C]. ...