怎样将一个double[,]存到xx.txt,怎样从xx.txt中读出这些数字到一个double[,]

导出代码如下:

 1string strOutput = string.Empty;
 2double[,] v = new double[3,4];
 3for(int i=0;i<3;i++)    //3是行数,其实可以取到v中的行数的,我这里只是略过了
 4{
 5  for(int j=0; j<4;j++)
 6  {
 7    strOutput += v[i,j].ToString();
 8    if(j != 4 - 1)//不为每一行的最后一个值
 9      strOutput += "\t";
10  }

11  if(i != 3 - 1)//不为最后一行
12    strOutput += "\n";
13}

//这里,在输出strOutput字符串就可以了,

输出方法:

 1FileInfo myFile = FileInfo(strFileName);//导出文件的路径
 2StreamWriter sw = myFile.CreateText();   //创建一个新文件
 3sw.Write(strOutput);
 4sw.Close();
 5
 6读入的代码:
 7StreamReader sr = new StreamReader(strFileName);
 8string strText = sr.ReadToEnd();   //把内容读到strText变量中去
 9sr.Close();
10if(strText == null || strText.Trim() == string.Empty)
11  return;
12string[] strRows = strText.Split('\n');  //分离出不同的行
13if(strRows[0== null || strRows[0].Trim() == string.Empty)//判断第一行是否有值
14  return;
15string[] strCol = strRows[0].Split('\t');//分离第一行为不同的列
16int rowCount = strRows.Length;  //得到行数
17int colCount = strCol.Length;//得到列数,虽然只是第一列的列数,但是string[,]中每行的列数是一定相同的
18double[,] v = new double[rowCount, colCount];
19for(int i=0; i<rowCount; i++)
20  {
21    strCol = strRows[0].Split('\t');//分离每行,以得到每行的列
22    for(int j=0; j<colCount; j++)
23      v[i,j] = double.Parse(strCol[j]);
24  }

25

这里得到的v就是你想要的double[,]矩阵数组

posted @ 2005-06-03 17:20  冰戈  阅读(464)  评论(0)    收藏  举报