用C# 将坐标轴数组写入文件,并读出文件坐标数组,赋值给数组

2024-12-11 16:55:39
推荐回答(1个)
回答1:

public void Save()
{
Point[] pt = {new Point(-10,2), new Point(-9,24), new Point(-8,45)};
string filePath = @"D:\Point.txt";
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath))
{
for (int i = 0; i < pt.Length; i++)
{
sw.Write(pt[i].X);
sw.Write(",");
sw.Write(pt[i].Y);
sw.Write(" ");
}
}
}

public void Load()
{
string filePath = @"D:\Point.txt";
using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
{
string str = sr.ReadToEnd();
string[] ss = str.Split(new char[] { ' ', ',' });
int ptCount = ss.Length / 2;
Point[] pt = new Point[ptCount];
for (int i = 0; i < ptCount; i++)
{
pt[i].X = System.Convert.ToInt32(ss[2*i]);
pt[i].Y = System.Convert.ToInt32(ss[2 * i + 1]);
}
}
}