二进制和纯文本数据的读写
引用别人的,这里做个纪录
using System;
using System.IO;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
public static void Main(string[] args)
{
double[,] r = { { 2, 3 }, { 4, 5 } };
//文本方式
using (StreamWriter sw = new StreamWriter("1.txt", false))
{
foreach (double d in r)
{
sw.Write(d);
sw.Write(" ");
}
}
string[] temp = File.ReadAllText("1.txt", Encoding.Default).Split(new char[] { ' ' });
int i = 0;
for (int y = 0; y < 2; y++)
for (int x = 0; x < 2; x++)
{
r[y, x] = Double.Parse(temp[i]);
i++;
}
Console.WriteLine("");
//二进制方式
using (FileStream fs = new FileStream("1.bin", FileMode.Create, FileAccess.Write))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
foreach (double d in r)
{
bw.Write(d);
}
}
}
using (FileStream fs = new FileStream("1.bin", FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
for (int y = 0; y < 2; y++)
for (int x = 0; x < 2; x++)
{
r[y, x] = br.ReadDouble();
}
}
}
}
}
}

浙公网安备 33010602011771号