C#操作流之Stream
C#中流的操作用处还是很多的,怎么说呢,我觉得流很神秘,总有一种我学不会的感觉,至于它有多大用处,我也不知道,但是不得不学啊。
什么是流呢,官方解释是 提供字节序列的一般视图,咋一看,有点蒙,仔细一琢磨吧,还不如咋一看呢,我觉得吧,流就是一张网,而字节序列呢,就像是构成网的那一个一个的窟窿。
关于Stream这个类呢,它的属性啊,方法有很多,说实话,我觉得没必要都掌握,为什么呢,因为我根本记不住,所以就先记点暂时用的比较多的吧。
1: CanRead: 只读属性,判断该流是否能够读取:
2: CanSeek: 只读属性,判断该流是否支持跟踪查找
3: CanWrite: 只读属性,判断当前流是否可写
4: void Flush():其实吧,在用流去写文件的时候呢,数据流是不会立刻写入文件的,而是先到了缓冲区,当执行了这个方法后,缓冲区的数据才会写入文件。
6: Position属性:表示在流中的位置
7: abstract int Read(byte[] buffer, int offset, int count)
这个方法包含了3个关键的参数:缓冲字节数组,位移偏量和读取字节个数,每次读取一个字节后会返回一个缓冲区中的总字节数
第一个参数:这个数组相当于一个空盒子,Read()方法每次读取流中的一个字节将其放进这个空盒子中。(全部读完后便可使用buffer字节数组了)
第二个参数:表示位移偏量,告诉我们从流中哪个位置(偏移量)开始读取。
最后一个参数:就是读取多少字节数。
返回值便是总共读取了多少字节数.
8: abstract long Seek(long offset, SeekOrigin origin)
还记得Position属性么?其实Seek方法就是重新设定流中的一个位置,如下:
Stream. Seek(-3,Origin.End); 表示在流末端往前数第3个位置
Stream. Seek(0,Origin.Begin); 表示在流的开头位置
Stream. Seek(3,Orig`in.Current); 表示在流的当前位置往后数第三个位置
9: abstract void Write(byte[] buffer,int offset,int count)
这个方法包含了3个关键的参数:缓冲字节数组,位移偏量和读取字节个数
和read方法不同的是 write方法中的第一个参数buffer不再是null了,而是已经有了许多byte类型的数据,我们只需通过设置 offset和count来将buffer中的数据写入流中
10: virtual void Close()
关闭流并释放资源,在实际操作中,如果不用using的话,别忘了使用完流之后将其关闭这个方法特别重要,使用完当前流千万别忘记关闭!
static void Main(string[] args) { byte[] buffer = null; string testString = "I am learning Stream!"; char[] readCharArray = null; byte[] readBuffer = null; string readString = string.Empty; //关于MemoryStream 我会在后续章节详细阐述 using (MemoryStream stream = new MemoryStream()) { Console.WriteLine("初始字符串为:{0}", testString); //如果该流可写 if (stream.CanWrite) { //首先我们尝试将testString写入流中 //需要把字符串转变为字节数组 buffer = Encoding.Default.GetBytes(testString); //从该数组的第一个位置开始写,长度为3,写完之后 stream中便有了数据 stream.Write(buffer, 0,3); Console.WriteLine("现在Stream.Postion在第{0}位置",stream.Position+1); //从刚才结束的位置(当前位置)往后移3位,到第7位 long newPositionInStream =stream.CanSeek? stream.Seek(3, SeekOrigin.Current):0; Console.WriteLine("重新定位后Stream.Postion在第{0}位置", newPositionInStream+1); if (newPositionInStream < buffer.Length) { //将从新位置(第7位)一直写到buffer的末尾,注意下stream已经写入了3个数据“Str” stream.Write(buffer, (int)newPositionInStream, buffer.Length - (int)newPositionInStream); } //写完后将stream的Position属性设置成0,开始读流中的数据 stream.Position = 0; // 设置一个空的盒子来接收流中的数据,长度根据stream的长度来决定 readBuffer = new byte[stream.Length]; //设置stream总的读取数量 , //注意!这时候流已经把数据读到了readBuffer中 int count = stream.CanRead?stream.Read(readBuffer, 0, readBuffer.Length):0; //由于刚开始时我们使用加密Encoding的方式,所以我们必须解密将readBuffer转化成Char数组,这样才能重新拼接成string //首先通过流读出的readBuffer的数据求出从相应Char的数量 int charCount = Encoding.Default.GetCharCount(readBuffer, 0, count); //通过该Char的数量 设定一个新的readCharArray数组 readCharArray = new char[charCount]; //Encoding 类的强悍之处就是不仅包含加密的方法,甚至将解密者都能创建出来(GetDecoder()), //解密者便会将readCharArray填充(通过GetChars方法,把readBuffer 逐个转化将byte转化成char,并且按一致顺序填充到readCharArray中) Encoding.Default.GetDecoder().GetChars(readBuffer, 0, count, readCharArray, 0); for (int i = 0; i < readCharArray.Length; i++) { readString += readCharArray[i]; } Console.WriteLine("读取的字符串为:{0}", readString); } stream.Close(); } Console.ReadLine(); }
注意事项:
1、无论什么流,用完一定要关闭,建议使用using方式
2、Stream的read和write是有区别的,除了在工能上以外,在用法上也要清楚,首先对于read(),一帮情况下是将整个流的字节数,也就是Stream.Length那么多的字节读到那个字节数组buffer中,所以嘛,要想执行read方法, 就需要一个空的字节数组,长度就是流Stream的长度,另外还要注意的是,从流里读出来的是字节数组,应 该先把它转成字符数组,然后再转成字符串或者进行其他的操作对于Write(),就是将字节数组写入流里,所以需 要知道有数据的字节数组,然后才能把这个字节数组写入到流里。
//--------------------------处理计数文件------------------------------------ string basePath = AppDomain.CurrentDomain.BaseDirectory; string zipName = "zip_" + DateTime.Now.ToString("yyyyMMdd"); string bcpName = "bcp_" + DateTime.Now.ToString("yyyyMMdd"); if (!File.Exists(Path.Combine(basePath, zipName + ".txt")) && !File.Exists(Path.Combine(basePath, bcpName + ".txt"))) { //删除根目录下所有的计数文件 File.Delete(Path.Combine(basePath, "zip_" + DateTime.Now.AddDays(-1).ToString("yyyyMMdd") + ".txt")); File.Delete(Path.Combine(basePath, "bcp_" + DateTime.Now.AddDays(-1).ToString("yyyyMMdd") + ".txt")); //创建当前日前的计数文件 using (StreamWriter sw = new StreamWriter(Path.Combine(basePath, zipName + ".txt"), true, Encoding.UTF8)) { sw.WriteLine(0); } using (StreamWriter sw = new StreamWriter(Path.Combine(basePath, bcpName + ".txt"), true, Encoding.UTF8)) { sw.WriteLine(0); } } //--------------------------处理计数文件------------------------------------ string sourceFileRootPath = string.Format(@"{0}data\", SourceRootPath); string sourceFileRootPath2 = string.Format(@"{0}data2\", SourceRootPath); string[] fileList = Directory.GetFiles(sourceFileRootPath, "*.bcp", SearchOption.AllDirectories); if (fileList != null && fileList.Length > 0) { string zipFileName = string.Format("data_{0}.zip", ConvertDateTimeInt(DateTime.Now)); string zipFile = string.Format("{0}{1}", SourceRootPath, zipFileName); //避免读写冲突 Directory.Move(sourceFileRootPath, sourceFileRootPath2); //重命名文件名(计数) string[] fileList2 = Directory.GetFiles(sourceFileRootPath2, "*.bcp", SearchOption.AllDirectories); foreach (string fileName in fileList2) { int count = Convert.ToInt32(File.ReadAllText(Path.Combine(basePath, bcpName + ".txt"))); File.Move(fileName, fileName.Replace(".bcp", "_" + GetFormat(count + 1) + ".bcp")); File.WriteAllText(Path.Combine(basePath, bcpName + ".txt"),(count + 1).ToString()); } CompressFile(sourceFileRootPath2, zipFile); Tiexue.General.Exception.Debug("一次压缩处理"); string[] zipFileList = Directory.GetFiles(SourceRootPath, "*.zip"); //重命名压缩包 foreach (string zipTemFile in zipFileList) { int count = Convert.ToInt32(File.ReadAllText(Path.Combine(basePath, zipName + ".txt"))); File.Move(zipTemFile, zipTemFile.Replace(".zip", "_" + GetFormat(count + 1) + ".zip")); File.WriteAllText(Path.Combine(basePath, zipName + ".txt"), (count + 1).ToString()); }
浙公网安备 33010602011771号