T
Terry
In the code below, I open a file using filestream, copy it to a byte
array, and the write that array out to a new file. But when I check
the size of the original file and the new file, the new file has one
less byte than the original file (although the file itself still works
fine). I was hoping someone could explain what is going on (why is
there a byte missing, and what might that byte be).
string originalFile = @"C:\MP3Test\Original.mp3";
string copiedFile = @"C:\MP3Test\Copied.mp3";
// Read file into byte array
FileStream fs1 = new FileStream(originalFile, FileMode.OpenOrCreate,
FileAccess.Read);
byte[] mp3ByteArray = new byte[fs1.Length];
fs1.Read(mp3ByteArray, 0, System.Convert.ToInt32(fs1.Length));
fs1.Close();
// Write same byte array to new file
int arraySize = new int();
arraySize = mp3ByteArray.GetUpperBound(0);
FileStream fs2 = new FileStream(copiedFile, FileMode.OpenOrCreate,
FileAccess.Write);
fs2.Write(mp3ByteArray, 0, arraySize);
fs2.Close();
// Compare size of original an new file
FileInfo fileInfo1 = new FileInfo(originalFile);
FileInfo fileInfo2 = new FileInfo(copiedFile);
Console.WriteLine("Size of original file: " +
fileInfo1.Length.ToString());
// output====> 20629 bytes
Console.WriteLine("Size of copied file: " +
fileInfo2.Length.ToString());
// output====> 20628 bytes. One byte less than original file.
Thanks,
Terry
array, and the write that array out to a new file. But when I check
the size of the original file and the new file, the new file has one
less byte than the original file (although the file itself still works
fine). I was hoping someone could explain what is going on (why is
there a byte missing, and what might that byte be).
string originalFile = @"C:\MP3Test\Original.mp3";
string copiedFile = @"C:\MP3Test\Copied.mp3";
// Read file into byte array
FileStream fs1 = new FileStream(originalFile, FileMode.OpenOrCreate,
FileAccess.Read);
byte[] mp3ByteArray = new byte[fs1.Length];
fs1.Read(mp3ByteArray, 0, System.Convert.ToInt32(fs1.Length));
fs1.Close();
// Write same byte array to new file
int arraySize = new int();
arraySize = mp3ByteArray.GetUpperBound(0);
FileStream fs2 = new FileStream(copiedFile, FileMode.OpenOrCreate,
FileAccess.Write);
fs2.Write(mp3ByteArray, 0, arraySize);
fs2.Close();
// Compare size of original an new file
FileInfo fileInfo1 = new FileInfo(originalFile);
FileInfo fileInfo2 = new FileInfo(copiedFile);
Console.WriteLine("Size of original file: " +
fileInfo1.Length.ToString());
// output====> 20629 bytes
Console.WriteLine("Size of copied file: " +
fileInfo2.Length.ToString());
// output====> 20628 bytes. One byte less than original file.
Thanks,
Terry