C# 流介绍 (原发布 csdn 2017

1、FileStream

FileStream 详细介绍参考msdn

写数据:
1using (FileStream fs = new FileStream("File.FileStream", FileMode.Create, FileAccess.Write)) 2{ 3 for (int i = 0; i < Cycles; i++) 4 { 5 for (int j = 0; j < Length; j++) 6 { 7 dis[j] = i * Length + j; 8 } 9 Buffer.BlockCopy(dis, 0, byData, i * readCount, readCount); 10 } 11 fs.Write(byData, 0, byData.Length); 12}
读数据
1using (FileStream fs = new FileStream("File.FileStream", FileMode.Open, FileAccess.Read)) 2{ 3 for (int i = 0; i < Cycles; i++) 4 { 5 fs.Seek(i * readCount, SeekOrigin.Begin); 6 fs.Read(byData, 0, readCount); 7 dis = new double[Length]; 8 Buffer.BlockCopy(byData, i * readCount, dis, 0, readCount); 9 } 10}

2、BinaryWriter/BinaryReader

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

1using (BinaryWriter bw = new BinaryWriter(File.Open("File.Binary", FileMode.Create))) 2{ 3 byte[] data = new byte[Cycles * readCount]; 4 for (int i = 0; i < Cycles; i++) 5 { 6 for (int j = 0; j < Length; j++) 7 { 8 dis[j] = i * Length + j; 9 } 10 Buffer.BlockCopy(dis, 0, data, i * readCount, readCount); 11 } 12 bw.Write(data); 13}

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

1using (BinaryReader wr = new BinaryReader(File.Open("File.Binary", FileMode.Open))) 2{ 3 for (int i = 0; i < Cycles; i++) 4 { 5 var readData = wr.ReadBytes(readCount); 6 dis = new double[Length]; 7 Buffer.BlockCopy(readData, 0, dis, 0, readCount); 8 } 9}

3、StreamWriter/StreamReader

3.1 StreamWriter 详细介绍参考msdn

1 using (StreamWriter sw = new StreamWriter("File.Stream", false, Encoding.GetEncoding("utf-16"))) 2 { 3 StringBuilder sb = new StringBuilder(); 4 for (int i = 0; i < Cycles; i++) 5 { 6 for (int j = 0; j < Length; j++) 7 { 8 dis[j] = i * Length + j; 9 sb.AppendFormat("{0},", dis[j]); 10 } 11 sb.AppendFormat("\n"); 12 } 13 sw.WriteLine(sb); 14 }

3.2 StreamReader 详细介绍参考msdn

1 using (StreamReader sd = new StreamReader("File.Stream", Encoding.GetEncoding("utf-16"))) 2 { 3 for (int i = 0; i < Cycles; i++) 4 { 5 string[] ch = sd.ReadLine().Split(new Char[] { ',' }, 6 System.StringSplitOptions.RemoveEmptyEntries); 7 for (int j = 0; j < Length; j++) 8 { 9 double.TryParse(ch[j], out dis[j]); 10 } 11 } 12 }

4 完整测试代码:

1class Program 2{ 3 static void Main() 4 { 5 fileReadAndWrite.BinaryWriterMethod(); 6 fileReadAndWrite.BinaryReaderMethod(); 7 8 fileReadAndWrite.FileStreamWriterMethod(); 9 fileReadAndWrite.FileStreamReadMethod(); 10 11 fileReadAndWrite.StreamWriterMethod(); 12 fileReadAndWrite.StreamReaderMethod(); 13 14 Console.ReadKey(true); 15 } 16} 17 18 19class FileReadAndWrite 20{ 21 private const int Length = 1024; 22 private const int Cycles = 64; 23 private int readCount; 24 private byte[] byData; 25 private double[] dis; 26 27 public FileReadAndWrite() 28 { 29 readCount = Length * sizeof(double); 30 dis = new double[Length]; 31 byData = new byte[Cycles * Length * sizeof(double)]; 32 } 33 34 #region BinaryWriter\BinaryReader 35 public void BinaryWriterMethod() 36 { 37 using (BinaryWriter bw = new BinaryWriter(File.Open("File.Binary", FileMode.Create))) 38 { 39 byte[] data = new byte[Cycles * readCount]; 40 for (int i = 0; i < Cycles; i++) 41 { 42 for (int j = 0; j < Length; j++) 43 { 44 dis[j] = i * Length + j; 45 } 46 Buffer.BlockCopy(dis, 0, data, i * readCount, readCount); 47 } 48 bw.Write(data); 49 } 50 } 51 52 public void BinaryReaderMethod() 53 { 54 using (BinaryReader wr = new BinaryReader(File.Open("File.Binary", FileMode.Open))) 55 { 56 for (int i = 0; i < Cycles; i++) 57 { 58 var readData = wr.ReadBytes(readCount); 59 Buffer.BlockCopy(readData, 0, dis, 0, readCount); 60 } 61 } 62 } 63 #endregion 64 65 #region FileStream Read\Write 66 public void FileStreamWriterMethod() 67 { 68 using (FileStream fs = new FileStream("File.FileStream", FileMode.Create,FileAccess.Write)) 69 { 70 for (int i = 0; i < Cycles; i++) 71 { 72 for (int j = 0; j < Length; j++) 73 { 74 dis[j] = i * Length + j; 75 } 76 Buffer.BlockCopy(dis, 0, byData, i * readCount, readCount); 77 } 78 fs.Write(byData, 0, byData.Length); 79 } 80 } 81 82 public void FileStreamReadMethod() 83 { 84 using (FileStream fs = new FileStream("File.FileStream", FileMode.Open, FileAccess.Read)) 85 { 86 for (int i = 0; i < Cycles; i++) 87 { 88 fs.Seek(i * readCount, SeekOrigin.Begin); 89 fs.Read(byData, 0, readCount); 90 Buffer.BlockCopy(byData, i * readCount, dis, 0, readCount); 91 } 92 } 93 } 94 #endregion 95 96 #region StreamWriter\StreamReader 97 public void StreamWriterMethod() 98 { 99 using (StreamWriter sw = new StreamWriter("File.Stream", false, 100 Encoding.GetEncoding("utf-16"))) 101 { 102 StringBuilder sb = new StringBuilder(); 103 for (int i = 0; i < Cycles; i++) 104 { 105 for (int j = 0; j < Length; j++) 106 { 107 dis[j] = i * Length + j; 108 sb.AppendFormat("{0},", dis[j]); 109 } 110 sb.AppendFormat("\n"); 111 } 112 sw.WriteLine(sb); 113 } 114 } 115 116 public void StreamReaderMethod() 117 { 118 using (StreamReader sd = new StreamReader("File.Stream", Encoding.GetEncoding("utf-16"))) 119 { 120 for (int i = 0; i < Cycles; i++) 121 { 122 string[] ch = sd.ReadLine().Split(new Char[] { ',' }, 123 System.StringSplitOptions.RemoveEmptyEntries); 124 for (int j = 0; j < Length; j++) 125 { 126 double.TryParse(ch[j], out dis[j]); 127 } 128 } 129 } 130 } 131 #endregion 132}
点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java类封装成dll

@参考文章1(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fazure_sky_2014%2Farticle%2Fdetails%2F71915605),@参考文章2(https://www.oschina.net/action/GoToLink?u

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593

PyTorch之前向传播函数自动调用forward

参考:1. pytorch学习笔记(九):PyTorch结构介绍(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fu012436149%2Farticle%2Fdetails%2F70145598)2.pytorch学习笔记(七):pytorchh

EntityFramework GroupJoin

总而言之,GroupJoin是先Join后Group,对应的SQL也是先Join,然后通过内置LINQ操作分组.参考文档GroupJoin方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fmsdn.microsoft.com%2

ASP.NET Web.config Transformations

...参考文档WebDeploymentContentMapforVisualStudioandASP.NET(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fmsdn.microsoft.com%2Fenus%2Flibrary%2F

C# 流介绍 (原发布 csdn 2017 - HelloWorld