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
}
}
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
}
}