把它记录下来,希望对以后查询有所帮助。也希望大家给点意见,共同学习。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- namespace test3
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string FileName = "E://test.txt";
- FileStream fs;
- BinaryWriter mywriter;
- BinaryReader myreader;//二进制写入流与读取流。
- DateTime theDate;
- Int64 dateInticks;
- fs = new FileStream(FileName, FileMode.Create);
- mywriter = new BinaryWriter(fs);//二进制流写入数据,一般与文件流一起使用哦。。。
- theDate = DateTime.Now;
- do
- {
- mywriter.Write(theDate.ToUniversalTime().Ticks);//将一个时间转化为二进制数据流,并写入文本
- theDate = theDate.AddDays(1);
- } while (theDate < DateTime.Now.AddDays(7));
- mywriter.Close();
- fs.Close();
- Console.WriteLine("write success....");
- fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
- myreader = new BinaryReader(fs);//二进制流读取数据,一般与文件流一起使用哦。。。
- for (int i = 0; i < 7; i++)
- {
- dateInticks = myreader.ReadInt64();//第次都读取64个字节的数据。
- theDate = new DateTime(dateInticks);//将读取的二进制数据转化为时间类型
- Console.WriteLine("Time is:{0}", theDate.ToString());
- }
- Console.WriteLine("Read Over...");
- }
- }
- }