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运算符,平时项目中也有用到这两种符号,对于其效率也没有进行比较过,趁着假期有空,先看下效 ...
随机推荐
- 骚操作!曾经爱过!用 Python 清理收藏夹里已失效的网站
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 小詹&有乔木 PS:如有需要Python学习资料的小伙伴可 ...
- File操作,访问文件或目录的属性信息
package seday03; import java.io.File;//记得导入File /** * File的每一个实例用于表示文件系统中的一个文件或目录 * 使用File可以: * 1:访 ...
- XML的互相序列化对象
using System.Xml.Serialization; using System.IO; using System.Xml; namespace Common { public class X ...
- [转]C#操作Outlook
本文转自:https://blog.csdn.net/yanlovehan/article/details/8500449 //引用Microsoft.Office.Interop.Outlook.d ...
- 【Gradle】Gradle入门
Gradle入门 配置Gradle环境 安装之前确保已经安装配置好Java环境,要求JDK6以上,并且在环境变量里配置了JAVA_HOME,查看Java版本可以在终端输入如下命令: java -ver ...
- [Go] gocron源码阅读-go语言中的切片接口和类型综合
// getCommands func getCommands() []cli.Command { command := cli.Command{ Name: "web", Usa ...
- c# WF 第9节 button控件
本节内容: 1:实现实例 1:实现实例 每当点击一个确定就出现一个窗口,当点击最后的确定时,关闭所有的窗口. 实现: 步骤1:对Form 1 -Form3 依次进行如下设置: 步骤2 : 当每点击一个 ...
- jTopo介绍(一)
jTopo(Javascript Topology library)是一款完全基于HTML5 Canvas的关系.拓扑图形化界面开发工具包.jTopo关注于数据的图形展示,它是面向开发人员的,需要进行 ...
- Linux 下 make 的时候,老是一堆warning
用下面的方法只显示error : 1) export CFLAGS="-w" 2) ./configure 3) make
- go 基本语法
====type==== import ( "fmt" ) func main() { type bigint byte //定义类型 var a bigint fmt.Print ...