C# 如何在将字节数组写入文件时添加新行

C# 如何在将字节数组写入文件时添加新行,c#,C#,嗨,我正在将音频文件读入字节数组。然后我想从该字节数组中读取每4个字节的数据,并将其写入另一个文件 我能够做到这一点,但我的问题是,每4字节的数据写入文件,我就想在后面添加新行。怎么做?? 这是我的密码 FileStream f = new FileStream(@"c:\temp\MyTest.acc"); for (i = 0; i < f.Length; i += 4) { byte[] b = new byte[4]; int bytesRead = f.Read(

嗨,我正在将音频文件读入字节数组。然后我想从该字节数组中读取每4个字节的数据,并将其写入另一个文件

我能够做到这一点,但我的问题是,每4字节的数据写入文件,我就想在后面添加新行。怎么做?? 这是我的密码

FileStream f = new FileStream(@"c:\temp\MyTest.acc");
for (i = 0; i < f.Length; i += 4)
{
    byte[] b = new byte[4];
    int bytesRead = f.Read(b, 0, b.Length);

    if (bytesRead < 4)
    {
        byte[] b2 = new byte[bytesRead];
        Array.Copy(b, b2, bytesRead);
        arrays.Add(b2);
    }
    else if (bytesRead > 0)
        arrays.Add(b);

    fs.Write(b, 0, b.Length);
}
FileStream f=newfilestream(@“c:\temp\MyTest.acc”);
对于(i=0;i0)
增加(b);
fs.Write(b,0,b.长度);
}

请提供任何建议。

我想这可能就是你问题的答案:

            byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
            fs.Write(newline, 0, newline.Length);
所以你的代码应该是这样的:

            FileStream f = new FileStream("G:\\text.txt",FileMode.Open);
            for (int i = 0; i < f.Length; i += 4)
            {
                byte[] b = new byte[4];
                int bytesRead = f.Read(b, 0, b.Length);

                if (bytesRead < 4)
                {
                    byte[] b2 = new byte[bytesRead];
                    Array.Copy(b, b2, bytesRead);
                    arrays.Add(b2);
                }
                else if (bytesRead > 0)
                    arrays.Add(b);

                fs.Write(b, 0, b.Length);
                byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
                fs.Write(newline, 0, newline.Length);
            }
FileStream f=newfilestream(“G:\\text.txt”,FileMode.Open);
对于(int i=0;i0)
增加(b);
fs.Write(b,0,b.长度);
byte[]newline=Encoding.ASCII.GetBytes(Environment.newline);
fs.Write(换行符,0,换行符.Length);
}

System.Environment.NewLine
传递到文件流


有关更多信息

Hi Nick…aft使用换行符,当我使用Hex Editor New打开文件时,数据显示为ff f1 58 40 0d 0a 28 41 4c 01 0d 0a。在4字节而不是新行之后,它被表示为0d0a,然后显示后4字节0d0a。这些是新行的字节表示。在记事本里打开它你会看到好的…是的。我可以看到4字节后的数据,它正在写入新的,如…谢谢。