Csharp流介绍

本文最后更新于 2025-11-16 22:08:05

FileStream

FileStream 详细介绍参考msdn

写数据:

1
2
3
4
5
6
7
8
9
10
11
12
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);
}

读数据

1
2
3
4
5
6
7
8
9
10
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);
}
}

BinaryWriter/BinaryReader

BinaryWriter

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

1
2
3
4
5
6
7
8
9
10
11
12
13
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);
}

BinaryReader

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

1
2
3
4
5
6
7
8
9
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);
}
}

StreamWriter/StreamReader

StreamWriter

详细介绍参考msdn

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
}

StreamReader

详细介绍参考msdn

1
2
3
4
5
6
7
8
9
10
11
12
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]);
}
}
}

完整测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Program
{
static void Main()
{
fileReadAndWrite.BinaryWriterMethod();
fileReadAndWrite.BinaryReaderMethod();

fileReadAndWrite.FileStreamWriterMethod();
fileReadAndWrite.FileStreamReadMethod();

fileReadAndWrite.StreamWriterMethod();
fileReadAndWrite.StreamReaderMethod();

Console.ReadKey(true);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
}

Csharp流介绍
https://njit-77.github.io/2017/09/25/Csharp流介绍/
作者
njit-77
发布于
2017年9月25日
许可协议