Append Data to Byte Array

2019-03-27 03:17发布

问题:

Currently, I am reading data from a binary file (File.ReadAllBytes), converting this byte array into a string and appending data onto this string. Lastly, I am converting the string back into a byte array and writing the data back into a new file.

Yeah - this method is fairly idiotic, and I've been curious as to whether or not there is some way to append this new data onto the end of the byte array (in the form of a byte).

String s = @"C:\File.exe";
Byte b[] = File.ReadAllBytes(s);

String NewString = ConvertToString(b[]);

NewString = NewString + "Some Data";

b[] = ConvertToByteArray(NewString);
File.WriteAllBytes(b[]);

// ConvertToByteArray and ConvertToString represent functions that converts string > Byte > string.

What I would like to do:

b[] = file.readallbytes(s)
b = b + "new Data"
file.writeallbytes(b[])

Thank you very much for any insight on the matter.

回答1:

You should get used to working with Streams - in this case you could use a MemoryStream to achieve the exact same thing without all those nasty arrays.

byte[] bytes = File.ReadAllBytes(inFilePath);
using (MemoryStream ms = new MemoryStream())
{
    // You could also just use StreamWriter to do "writer.Write(bytes)"
    ms.Write(bytes, 0, bytes.Length);

    using (StreamWriter writer = new StreamWriter(ms))
    {
        writer.Write("Some Data");
    }

    File.WriteAllBytes(outFilePath, ms.ToArray());
}

Admitedly this looks a whole load more complicated than your code, but under the covers what it's doing is more efficient.

Of course if you are just writing to another file (or even the same file) you can simply write directly to the file and skip the need for a byte array or MemoryStream entirely - this is the beauty of streams.



回答2:

Use a List<Byte>, add every byte from the initial ReadAllBytes using AddRange, and then add the next set of bytes. Finally, use CopyTo to copy all bytes to an array (of size List.Length).



回答3:

Use FileStream, seek to the end of the file, then write what you need:

using (var fs = new FileStream(s, FileMode.Open, FileAccess.ReadWrite))
{
    fs.Seek(SeekOrigin.End);
    fs.Write(data, 0, data.Length);
}

If you really need to read the whole file, then just use Array.Resize to make your buffer bigger, and copy over the part you want to append.

var data = File.ReadAllBytes(s);
Array.Resize(ref data, data.Length + toAppend.Length);
Array.Copy(toAppend, 0, data, data.Length - toAppend.Length, toAppend.Length);
File.WriteAllBytes(s, data);

There's no "shorthand" for this, sorry. :\


If the entire point of this is to append a string, just use File.AppendAllText!



回答4:

If you just want to append something to the file, then you should be using a FileStream and a StreamWriter:

        using (var f = File.Open(@"C:\File.exe", FileMode.Append))
        using (var w = new StreamWriter(f))
            w.Write("new Data");

or something similar to this.

If you just want to append some bytes:

        using (var f = File.Open(@"C:\File.exe", FileMode.Append))
        {
            byte[] buf = new byte[] { 69, 65, 66, 67 };
            f.Write(buf, 0, buf.Length);
        }


回答5:

Create a new array with size of the sum of two separate arrays, then copy your arrays to it.