BinaryWriter.Write Method (Double)
.NET Framework 3.0
Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
For a list of common I/O tasks, see Common I/O Tasks.
The following code example shows how to read and write Double data to memory by using the BinaryReader and BinaryWriter classes on top of the MemoryStream class. MemoryStream only reads and writes Byte data.
using System; using System.IO; class BinaryRW { static void Main() { int i; const int arrayLength = 1000; // Create random data to write to the stream. Random randomGenerator = new Random(); double[] dataArray = new double[arrayLength]; for(i = 0; i < arrayLength; i++) { dataArray[i] = 100.1 * randomGenerator.NextDouble(); } using(BinaryWriter binWriter = new BinaryWriter(new MemoryStream())) { // Write the data to the stream. Console.WriteLine("Writing data to the stream."); for(i = 0; i < arrayLength; i++) { binWriter.Write(dataArray[i]); } // Create a reader using the stream from the writer. using(BinaryReader binReader = new BinaryReader(binWriter.BaseStream)) { try { // Return to the beginning of the stream. binReader.BaseStream.Position = 0; // Read and verify the data. Console.WriteLine("Verifying the written data."); for(i = 0; i < arrayLength; i++) { if(binReader.ReadDouble() != dataArray[i]) { Console.WriteLine("Error writing data."); break; } } Console.WriteLine("The data was written " + "and verified."); } catch(EndOfStreamException e) { Console.WriteLine("Error writing data: {0}.", e.GetType().Name); } } } } }
import System.*;
import System.IO.*;
class BinaryRW
{
public static void main(String[] args)
{
int i;
final int arrayLength = 1000;
// Create random data to write to the stream.
Random randomGenerator = new Random();
double dataArray[] = new double[arrayLength];
for(i = 0;i < arrayLength;i++) {
dataArray[i] = 100.1*randomGenerator.NextDouble() ;
}
BinaryWriter binWriter = new BinaryWriter(new MemoryStream());
try {
// Write the data to the stream.
Console.WriteLine("Writing data to the stream.");
for(i = 0;i < arrayLength;i++) {
binWriter.Write(dataArray [ i]);
}
// Create a reader using the stream from the writer.
BinaryReader binReader =
new BinaryReader(binWriter.get_BaseStream());
try {
try {
// Return to the beginning of the stream.
binReader.get_BaseStream().set_Position(0);
// Read and verify the data.
Console.WriteLine("Verifying the written data.");
for(i = 0;i < arrayLength;i++) {
if ( binReader.ReadDouble() != dataArray[i] ) {
Console.WriteLine("Error writing data.");
break;
}
}
Console.WriteLine(("The data was written "
+ "and verified."));
}
catch(EndOfStreamException e) {
Console.WriteLine("Error writing data: {0}.",
e.GetType().get_Name());
}
}
finally {
if (binReader != null) {
binReader = null;
}
}
}
finally {
if (binWriter != null) {
binWriter = null;
}
}
} //main
} //BinaryRW
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.