So you want to write two arrays to a file. You can either concatenate your two arrays into a single array and write that (similar to un-lucky's answer) or you can write the arrays in two separate operations (like fryday's answer). Both of those are perfectly valid ways to do it. Although combining the arrays just for the purpose of writing it to a file is not as efficient, obviously.
Your bigger problem is what you are going to do when you want to read back that data. When you go to read it back, it's just going to look like one big array. How do you know where one array ends and the other starts?
There are a couple of ways you can think about handling that. If the arrays have a know, fixed size, then it's not that big of a deal. For example, if you know bytes
is always 100 bytes long and farArray is always 200 bytes long (for example) then you can read it like this:
var bytes = new byte[100];
var farArray = new byte[200];
stream.Read(bytes, 0, 100);
stream.Read(farArray, 0, 200);
Easy. Now if your arrays aren't fixed length. Then probably the easiest solution would be to include a header at the start of the file that will contain the lengths of the two arrays. So when you write, you'd do something like:
stream.Write(BitConverter.GetBytes(bytes.Length), 0, 4);
stream.Write(BitConverter.GetBytes(farArray.Length), 0, 4);
stream.Write(bytes, 0, bytes.Length);
stream.Write(farArray, 0, farArray.Length);
Now the first 8 bytes (two Int32's) of your file will contain the lengths of the two arrays that follow. So to read you'd do something like:
var byteLengthArray = new byte[4];
var farLengthArray = new byte[4];
stream.Read(byteLengthArray, 0, 4);
stream.Read(farLengthArray, 0, 4);
// Note: you may have to worry about endianness if a file is written
// with one architecture and read with another.
var byteLength = BitConverter.ToInt32(byteLengthArray, 0);
var farLength = BitConverter.ToInt32(farLengthArray, 0);
// now we know how big the two arrays are!
var bytes = new byte[byteLength];
var farArray = new byte[farLength];
stream.Read(bytes, 0, byteLength);
stream.Read(farArray, 0, farLength);
Another way you could do it, although it would make reading even more fiddly, is to include some byte or group of bytes as a delimiter in between the two arrays. To do this you must be sure that the delimiter cannot occur anywhere else in either of the arrays. So you'd write something like:
stream.Write(bytes, 0, bytes.Length);
stream.Write(delimiter, 0, delimiter.Length);
stream.Write(farArray, 0, farArray.Length);
Now reading this is a bit tricky. Probably the easiest way would be to read all the bytes, and then search for the delimiter string. Once you find the place (index) where the delimiter starts, you would copy all the bytes before it into bytes
and all the bytes after (taking into account the length of the delimiter array) into farArray
.
Of course, the easier way to avoid all this tricky and error-prone byte copying is to just use the serialization and deserialization tools provided by the framework. Unless you have a very good reason (like you need your files to be ultra-compact for example), you probably shouldn't invent your own file formats.