BinaryReader,BinaryWriter
1 using System;
2 using System.IO;
3 using System.Globalization;
4 using System.Text;
5 using System.Data;
6 using System.Xml;
7
8
9 public class BinStream
10 {
11 public BinStream()
12 {
13 Writer();
14 Reader();
15 //ReaderWriteFile();
16 }
17 public static void Main()
18 {
19 BinStream bs = new BinStream();
20 Console.ReadLine();
21
22
23
24 }
25
26
27 private void BinaryReaderWriterDemo()
28 {
29
30 string FileName = "SampleDates.txt";
31
32 FileStream myStream;
33
34 BinaryWriter myWriter;
35
36 BinaryReader myReader;
37
38 DateTime theDate;
39
40 Int64 dateInTicks;
41
42 myStream = new FileStream(FileName, FileMode.OpenOrCreate,
43 FileAccess.Write, FileShare.ReadWrite);
44
45 myWriter = new BinaryWriter(myStream);
46
47 theDate = DateTime.Now;
48
49 do
50 {
51
52 myWriter.Write(theDate.ToUniversalTime().Ticks);
53
54 theDate = theDate.AddDays(1);
55
56 } while (theDate < DateTime.Now.AddDays(7));
57
58 myWriter.Close();
59
60 myStream.Close();
61 Console.WriteLine("Data Written!");
62 Console.WriteLine(theDate.ToUniversalTime().Ticks.ToString());
63 Console.WriteLine();
64 myStream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
65
66 myReader = new BinaryReader(myStream);
67 myReader.BaseStream.Seek(0, SeekOrigin.Begin);
68 for (int i = 0; i < 7; i++)
69 {
70
71 dateInTicks = (myReader.ReadInt64());
72
73 theDate = new DateTime(dateInTicks);
74
75 Console.WriteLine("{0} ticks is {1}", dateInTicks, theDate.ToLongTimeString());
76
77 Console.WriteLine(theDate.ToLongDateString());
78
79 }
80
81 myReader.Close();
82
83 myStream.Close();
84
85 }
86
87
88 private void Writer()
89 {
90 try
91 {
92 Console.Out.WriteLine("Preparing to Write ...");
93 //Open a FileStream on the file "aboutme"
94 FileStream fout = new FileStream("aboutme.txt", FileMode.OpenOrCreate,
95 FileAccess.Write, FileShare.ReadWrite);
96 //Create a BinaryWriter from the FileStream
97 BinaryWriter bw = new BinaryWriter(fout);
98 //Create some arbitrary variables
99 string name = "Saurabh塗聚文,涂斯博。";
100 int age = 20;
101 double height = 5.11;
102 bool single = true;
103 char gender = 'M';
104 //Write the values to file
105 bw.Write(name);
106 bw.Write(age);
107 bw.Write(height);
108 bw.Write(single);
109 bw.Write(gender);
110 //Close the file and free resources
111 bw.Close();
112 Console.WriteLine("Data Written!");
113 Console.WriteLine();
114
115 string utf8String = "anqing:瀹夊簡";//ASCII中文
116
117 // Create two different encodings.
118 Encoding utf8= Encoding.UTF8;
119 Encoding defaultCode= Encoding.Default;
120
121 // Convert the string into a byte[].
122 byte[] utf8Bytes = defaultCode.GetBytes(utf8String);
123
124 // Perform the conversion from one encoding to the other.
125 byte[] defaultBytes = Encoding.Convert(utf8, defaultCode, utf8Bytes );
126
127 // Convert the new byte[] into a char[] and then into a string.
128 // This is a slightly different approach to converting to illustrate
129 // the use of GetCharCount/GetChars.
130 char[] defaultChars = new char[defaultCode.GetCharCount(defaultBytes , 0, defaultBytes .Length)];
131 defaultCode.GetChars(defaultBytes , 0, defaultBytes .Length, defaultChars , 0);
132 string defaultString = new string(defaultChars );
133
134 // Display the strings created before and after the conversion.
135 Console.WriteLine("Original string: {0}", utf8String);
136 Console.WriteLine("Ascii converted string: {0}", defaultString);
137
138 //或者如下:
139 //byte[] buffer1 = Encoding.Default.GetBytes(utf8String );
140 //byte[] buffer2 = Encoding.Convert(Encoding.UTF8, Encoding.Default, buffer1, 0, buffer1.Length);
141 //string strBuffer = Encoding.Default.GetString(buffer2, 0, buffer2.Length);
142
143
144 string testfile = @"weatherutf.txt";//直接看文件是亂碼
145
146 // create a test file using BinaryWriter
147 FileStream fs = File.Create(testfile);
148 UTF8Encoding utf = new UTF8Encoding();
149
150 BinaryWriter bws = new BinaryWriter(fs, utf8);
151 string bstring;
152
153 bstring = "This is line #1 of text 涂聚文,塗鴉written as a binary stream.\r\n";
154 bstring += "This is line #2 of text written as a binary stream.\r\n";
155 bstring += "This is line #3 of text written as a binary stream.";
156 bws.Write(bstring);
157
158 // reset the stream position for reading
159 fs.Seek(0, SeekOrigin.Begin);
160
161 // Read the string as raw bytes using FileStream...
162 // The first series of bytes is the UTF7 encoded length of the
163 // string. In this case, however, it is just the first two bytes.
164 int len = fs.ReadByte() & 0x7f;
165 len += fs.ReadByte() * 0x80;
166 // read the string as bytes of length = len
167 byte[] rawbytes = new byte[len];
168 fs.Read(rawbytes, 0, len);
169
170 // display the string
171 Console.WriteLine("Read from FileStream: \n" + utf8.GetString(rawbytes));
172 Console.WriteLine();
173
174 // Now, read the string using BinaryReader
175
176 // reset the stream position for reading again
177 fs.Seek(0, SeekOrigin.Begin);
178
179 BinaryReader br = new BinaryReader(fs, utf8);
180 // ReadString will read the length prefix and return the string.
181 bstring = br.ReadString();
182 Console.WriteLine("Read from BinaryReader: \n" + bstring);
183 fs.Close();
184
185
186 }
187 catch (IOException e)
188 {
189 Console.WriteLine("An IO Exception Occurred :" + e);
190 }
191 }
192 private void Reader()
193 {
194 try
195 {
196 Console.WriteLine("Preparing to Read ...");
197 //Open a FileStream in Read mode
198 FileStream fin = new FileStream("aboutme.txt", FileMode.Open,
199 FileAccess.Read, FileShare.ReadWrite);
200 //Create a BinaryReader from the FileStream
201 BinaryReader br = new BinaryReader(fin);
202 //Seek to the start of the file
203 br.BaseStream.Seek(0, SeekOrigin.Begin);
204 //Read from the file and store the values to the variables
205 string name = br.ReadString();
206 int age = br.ReadInt32();
207 double height = br.ReadDouble();
208 bool single = br.ReadBoolean();
209 char gender = br.ReadChar();
210 //Display the data on the console
211 Console.WriteLine("Name :" + name);
212 Console.WriteLine("Age :" + age);
213 Console.WriteLine("Height :" + height);
214 Console.WriteLine("Single? :" + single);
215 Console.WriteLine("Gender M/F:" + gender);
216 //Close the stream and free the resources
217 br.Close();
218 Console.WriteLine("Data Read!");
219 }
220 catch (IOException e)
221 {
222 Console.WriteLine("An IO Exception Occurred :" + e);
223 }
224 }
225 }
2 using System.IO;
3 using System.Globalization;
4 using System.Text;
5 using System.Data;
6 using System.Xml;
7
8
9 public class BinStream
10 {
11 public BinStream()
12 {
13 Writer();
14 Reader();
15 //ReaderWriteFile();
16 }
17 public static void Main()
18 {
19 BinStream bs = new BinStream();
20 Console.ReadLine();
21
22
23
24 }
25
26
27 private void BinaryReaderWriterDemo()
28 {
29
30 string FileName = "SampleDates.txt";
31
32 FileStream myStream;
33
34 BinaryWriter myWriter;
35
36 BinaryReader myReader;
37
38 DateTime theDate;
39
40 Int64 dateInTicks;
41
42 myStream = new FileStream(FileName, FileMode.OpenOrCreate,
43 FileAccess.Write, FileShare.ReadWrite);
44
45 myWriter = new BinaryWriter(myStream);
46
47 theDate = DateTime.Now;
48
49 do
50 {
51
52 myWriter.Write(theDate.ToUniversalTime().Ticks);
53
54 theDate = theDate.AddDays(1);
55
56 } while (theDate < DateTime.Now.AddDays(7));
57
58 myWriter.Close();
59
60 myStream.Close();
61 Console.WriteLine("Data Written!");
62 Console.WriteLine(theDate.ToUniversalTime().Ticks.ToString());
63 Console.WriteLine();
64 myStream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
65
66 myReader = new BinaryReader(myStream);
67 myReader.BaseStream.Seek(0, SeekOrigin.Begin);
68 for (int i = 0; i < 7; i++)
69 {
70
71 dateInTicks = (myReader.ReadInt64());
72
73 theDate = new DateTime(dateInTicks);
74
75 Console.WriteLine("{0} ticks is {1}", dateInTicks, theDate.ToLongTimeString());
76
77 Console.WriteLine(theDate.ToLongDateString());
78
79 }
80
81 myReader.Close();
82
83 myStream.Close();
84
85 }
86
87
88 private void Writer()
89 {
90 try
91 {
92 Console.Out.WriteLine("Preparing to Write ...");
93 //Open a FileStream on the file "aboutme"
94 FileStream fout = new FileStream("aboutme.txt", FileMode.OpenOrCreate,
95 FileAccess.Write, FileShare.ReadWrite);
96 //Create a BinaryWriter from the FileStream
97 BinaryWriter bw = new BinaryWriter(fout);
98 //Create some arbitrary variables
99 string name = "Saurabh塗聚文,涂斯博。";
100 int age = 20;
101 double height = 5.11;
102 bool single = true;
103 char gender = 'M';
104 //Write the values to file
105 bw.Write(name);
106 bw.Write(age);
107 bw.Write(height);
108 bw.Write(single);
109 bw.Write(gender);
110 //Close the file and free resources
111 bw.Close();
112 Console.WriteLine("Data Written!");
113 Console.WriteLine();
114
115 string utf8String = "anqing:瀹夊簡";//ASCII中文
116
117 // Create two different encodings.
118 Encoding utf8= Encoding.UTF8;
119 Encoding defaultCode= Encoding.Default;
120
121 // Convert the string into a byte[].
122 byte[] utf8Bytes = defaultCode.GetBytes(utf8String);
123
124 // Perform the conversion from one encoding to the other.
125 byte[] defaultBytes = Encoding.Convert(utf8, defaultCode, utf8Bytes );
126
127 // Convert the new byte[] into a char[] and then into a string.
128 // This is a slightly different approach to converting to illustrate
129 // the use of GetCharCount/GetChars.
130 char[] defaultChars = new char[defaultCode.GetCharCount(defaultBytes , 0, defaultBytes .Length)];
131 defaultCode.GetChars(defaultBytes , 0, defaultBytes .Length, defaultChars , 0);
132 string defaultString = new string(defaultChars );
133
134 // Display the strings created before and after the conversion.
135 Console.WriteLine("Original string: {0}", utf8String);
136 Console.WriteLine("Ascii converted string: {0}", defaultString);
137
138 //或者如下:
139 //byte[] buffer1 = Encoding.Default.GetBytes(utf8String );
140 //byte[] buffer2 = Encoding.Convert(Encoding.UTF8, Encoding.Default, buffer1, 0, buffer1.Length);
141 //string strBuffer = Encoding.Default.GetString(buffer2, 0, buffer2.Length);
142
143
144 string testfile = @"weatherutf.txt";//直接看文件是亂碼
145
146 // create a test file using BinaryWriter
147 FileStream fs = File.Create(testfile);
148 UTF8Encoding utf = new UTF8Encoding();
149
150 BinaryWriter bws = new BinaryWriter(fs, utf8);
151 string bstring;
152
153 bstring = "This is line #1 of text 涂聚文,塗鴉written as a binary stream.\r\n";
154 bstring += "This is line #2 of text written as a binary stream.\r\n";
155 bstring += "This is line #3 of text written as a binary stream.";
156 bws.Write(bstring);
157
158 // reset the stream position for reading
159 fs.Seek(0, SeekOrigin.Begin);
160
161 // Read the string as raw bytes using FileStream...
162 // The first series of bytes is the UTF7 encoded length of the
163 // string. In this case, however, it is just the first two bytes.
164 int len = fs.ReadByte() & 0x7f;
165 len += fs.ReadByte() * 0x80;
166 // read the string as bytes of length = len
167 byte[] rawbytes = new byte[len];
168 fs.Read(rawbytes, 0, len);
169
170 // display the string
171 Console.WriteLine("Read from FileStream: \n" + utf8.GetString(rawbytes));
172 Console.WriteLine();
173
174 // Now, read the string using BinaryReader
175
176 // reset the stream position for reading again
177 fs.Seek(0, SeekOrigin.Begin);
178
179 BinaryReader br = new BinaryReader(fs, utf8);
180 // ReadString will read the length prefix and return the string.
181 bstring = br.ReadString();
182 Console.WriteLine("Read from BinaryReader: \n" + bstring);
183 fs.Close();
184
185
186 }
187 catch (IOException e)
188 {
189 Console.WriteLine("An IO Exception Occurred :" + e);
190 }
191 }
192 private void Reader()
193 {
194 try
195 {
196 Console.WriteLine("Preparing to Read ...");
197 //Open a FileStream in Read mode
198 FileStream fin = new FileStream("aboutme.txt", FileMode.Open,
199 FileAccess.Read, FileShare.ReadWrite);
200 //Create a BinaryReader from the FileStream
201 BinaryReader br = new BinaryReader(fin);
202 //Seek to the start of the file
203 br.BaseStream.Seek(0, SeekOrigin.Begin);
204 //Read from the file and store the values to the variables
205 string name = br.ReadString();
206 int age = br.ReadInt32();
207 double height = br.ReadDouble();
208 bool single = br.ReadBoolean();
209 char gender = br.ReadChar();
210 //Display the data on the console
211 Console.WriteLine("Name :" + name);
212 Console.WriteLine("Age :" + age);
213 Console.WriteLine("Height :" + height);
214 Console.WriteLine("Single? :" + single);
215 Console.WriteLine("Gender M/F:" + gender);
216 //Close the stream and free the resources
217 br.Close();
218 Console.WriteLine("Data Read!");
219 }
220 catch (IOException e)
221 {
222 Console.WriteLine("An IO Exception Occurred :" + e);
223 }
224 }
225 }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
标签:
csharp
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!