Program.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. namespace BinnaryfFileSave
  2. {
  3. using System;
  4. using System.IO;
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. int count = int.MaxValue/2;
  10. double[] doubleArray = new double[count];
  11. Parallel.For(0, count, i =>
  12. {
  13. double angle = i * 2 * Math.PI / count;
  14. doubleArray[i] = Math.Sin(angle);
  15. });
  16. // 写入double数组到二进制文件
  17. WriteDoubleArrayToBinaryFile("doubleArray.bin", doubleArray);
  18. // 从二进制文件读取double数组
  19. double[] readDoubleArray = ReadDoubleArrayFromBinaryFile("doubleArray.bin");
  20. Console.WriteLine("原始 double 数组:");
  21. foreach (double value in doubleArray)
  22. {
  23. Console.Write(value + " ");
  24. }
  25. Console.WriteLine();
  26. Console.WriteLine("从文件读取的 double 数组:");
  27. foreach (double value in readDoubleArray)
  28. {
  29. Console.Write(value + " ");
  30. }
  31. Console.WriteLine();
  32. }
  33. static void WriteDoubleArrayToBinaryFile(string filePath, double[] array)
  34. {
  35. using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
  36. {
  37. foreach (double value in array)
  38. {
  39. writer.Write(value);
  40. }
  41. }
  42. }
  43. static double[] ReadDoubleArrayFromBinaryFile(string filePath)
  44. {
  45. using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
  46. {
  47. long fileSize = new FileInfo(filePath).Length;
  48. int elementCount = (int)(fileSize / sizeof(double));
  49. double[] array = new double[elementCount];
  50. for (int i = 0; i < elementCount; i++)
  51. {
  52. array[i] = reader.ReadDouble();
  53. }
  54. return array;
  55. }
  56. }
  57. }
  58. }