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 }
posted @ 2011-04-27 16:45  ®Geovin Du Dream Park™  阅读(510)  评论(0编辑  收藏  举报