一、在FileStream流和byte[]中查找(搜索)指定字符串
/// <summary> /// 在当前流中搜索指定的 byte[] /// </summary> /// <param name="arr"></param> /// <param name="key">搜索关键字</param> /// <param name="beginPosition">搜索开始位置</param> /// <returns>如果存在则返回byte[]在流中首次出现的位置,否则返回 -1</returns> private static List<long> Search(FileStream stream, long beginPosition, long endPosition, byte[] key) { //FileStream stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); List<long> position = new List<long>(); if (stream == null || stream.Length <= beginPosition) { return null; } if (key == null || stream.Length < key.Length) { return null; } long i = -1; long j = -1; int currentByte = int.MinValue; //for (i = beginPosition; i < stream.Length; i++) for (i = beginPosition; i < endPosition; i++) { if (stream.Length < key.Length + i) { break; } stream.Seek(i, SeekOrigin.Begin); for (j = 0; j < key.Length; j++) { currentByte = stream.ReadByte(); if (currentByte != key[j]) { break; } } if (j == key.Length) { //return i; position.Add(i); } if (currentByte == -1) { break; } } return position; }
参考:https://blog.csdn.net/lonelyenvoy/article/details/52416529
标签:stream,C#,List,汇总,FileStream,搜索,byte From: https://www.cnblogs.com/Mars-0603/p/17613642.html