c# 写入字节流文件
public void WriteData(byte[] data)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
path += "\\dat";
if (!string.IsNullOrEmpty(path))
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path + "\\" + filename + ".dat";
if (!File.Exists(path))
{
FileStream fs = File.Create(path);
fs.Close();
}
if (File.Exists(path))
{
FileStream file = new FileStream(path, FileMode.Append);//以追加的形式打开文件
file.Write(data, 0, data.Length);//写入byte[]型数据
file.Flush();
file.Close();
}
}
}
其二
FileStream fs = new FileStream(path, FileMode.Append); //初始化FileStream对象
BinaryWriter bw = new BinaryWriter(fs); //创建BinaryWriter对象
//FileStream file = new FileStream(path, FileMode.Append);//以追加的形式打开文件
foreach (var item in list)
{
bw.Write(item);//写入byte[]型数据
}
//file.Flush();
bw.Close();
fs.Close();
学习到了,博主好厉害!