How do I read a raw byte array from any file, and write that byte array back into a new file?
+2
A:
(edit: note that the question changed; it didn't mention byte[]
initially; see revision 1)
Well, File.Copy
leaps to mind; but otherwise this sounds like a Stream
scenario:
using (Stream source = File.OpenRead(inPath))
using (Stream dest = File.Create(outPath)) {
byte[] buffer = new byte[2048]; // pick size
int bytesRead;
while((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) {
dest.Write(buffer, 0, bytesRead);
}
}
Marc Gravell
2009-09-20 07:59:02
+4
A:
Do you know about TextReader and TextWriter, and their descendents StreamReader and StreamWriter? I think these will solve your problem because they handle encodings, BinaryReader does not know about encodings or even text, it is only concerned with bytes.
Dale Halliwell
2009-09-20 07:59:46
Jeremy asked about reading and writing BINARY files, didn't he?
pavium
2009-09-20 08:02:59
If it is just binary data, then why is there any problem with the char encoding?
Dale Halliwell
2009-09-20 08:09:35
+4
A:
byte[] data = File.ReadAllBytes(path1);
File.WriteAllBytes(path2, data);
Tommy Carlier
2009-09-20 08:07:05