C# 流介绍 (原发布 csdn 2017-09-15 23:37:52)
2020-04-02 18:50
1071 查看
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 }
转载于:https://www.cnblogs.com/njit-77/p/11469075.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- c# Equal函数 and 运算符'==' (原发布 csdn 2017年10月15日 20:39:26)
- 需要这样一个记事本-12 C#命名空间介绍
- VS向IIS发布C#或VB应用程序
- C#反射之一(Assembly.Load及Assembly.Load.CreateInstance方法介绍)
- flex介绍(摘自csdn)
- C#语言的简单介绍和.NET Framework
- Lua介绍极其在C#项目中的应用
- c#应用程序的发布
- C#数据类型详细的介绍
- C#开发COM组件供其他开发环境或工具调用介绍
- C#命令行编译器的步骤介绍
- 19.C#逐一介绍IEnumerable和IEnumerable<T>中的扩展方法(10.3-10.5)
- C# WebService介绍
- C#中的Dictionary字典类介绍
- 谁能给我介绍一本关于asp.net(C#)三层结构书。
- C#常用控件介绍使用
- C#语言参考--(1)介绍-1
- C# MSChart图表控件使用介绍
- 介绍FoxBpm如何实现发布连接器
- C#中的Dictionary字典类介绍