c# IO二进制文件流操作

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace FileOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();


            //第一次截取
            System.Collections.Generic.List<byte[]> s = p.ftest("E:\\data.bin");
            byte[] firstSpilt = s[0].Skip(35).Take(s[0].Length - 35).ToArray();
            byte[] addbyte = null;


            //第二次截取
            List<byte[]> t = p.SecondSpilt(firstSpilt);
            //byte[] sssfft = t[t.Count-1];
            try
            {
                p.writeData(t, "C:/ttt.bin");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        #region 读取文件
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        System.Collections.Generic.List<byte[]> ftest(string filename)
        {
            System.Collections.Generic.List<byte[]> array = new List<byte[]>();
            System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open);
            System.IO.BinaryReader read = new System.IO.BinaryReader(fs);
            try
            {
                int index = 0;
                while (fs.Length > index)
                {
                    int lenght = (int)fs.Length;
                    byte[] buff = read.ReadBytes(lenght);
                    array.Add(buff);
                    index += lenght;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return array;
        }
        #endregion


        #region 第二次转换
        /// <summary>
        /// 第二次转换
        /// </summary>
        /// <param name="bt"></param>
        /// <returns></returns>
        System.Collections.Generic.List<byte[]> SecondSpilt(byte[] bt)
        {
            System.Collections.Generic.List<byte[]> array = new List<byte[]>();


            MemoryStream ms = new MemoryStream(bt);
            System.IO.BinaryReader read = new System.IO.BinaryReader(ms);
            try
            {
                int index = 0;
                while (ms.Length > index)
                {
                    int lenght = (int)ms.Length - index < 8 ? (int)ms.Length - index : 8;
                    byte[] buff = read.ReadBytes(lenght);
                    byte[] buff2 = buff.Skip(2).Take(4).ToArray();
                    array.Add(buff2);
                    index += lenght;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return array;
        }




        #endregion


        #region 写入文件byte[]
        /// <summary>
        /// 写入文件byte[]
        /// </summary>
        /// <param name="bytedata"></param>
        /// <param name="filename"></param>
        public void writeData(byte[] bytedata, string filename)
        {
            using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open))
            {
                using (System.IO.BinaryWriter writer = new System.IO.BinaryWriter(fs))
                {
                    writer.Write(bytedata);
                    writer.Close();
                    fs.Close();
                }


            }


        }
        #endregion


        #region 写入文件string
        /// <summary>
        /// 写入文件string
        /// </summary>
        /// <param name="stringData"></param>
        /// <param name="filename"></param>
        public void writeData(string stringData, string filename)
        {
            System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open);
            System.IO.BinaryWriter writer = new System.IO.BinaryWriter(fs);
            writer.Write(stringData);
            writer.Close();
        }
        #endregion


        #region 写入文件List<byte[]>
        /// <summary>
        /// 写入文件List<byte[]>
        /// </summary>
        /// <param name="bytedata"></param>
        /// <param name="filename"></param>
        public void writeData(List<byte[]> listBytedata, string filename)
        {
            using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open))
            {
                using (System.IO.BinaryWriter writer = new System.IO.BinaryWriter(fs))
                {
                    foreach (byte[] s in listBytedata)
                    {
                        writer.Write(s);


                    }
                    writer.Close();
                    fs.Close();
                }


            }


        }
        #endregion


      


    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值