怎样将一个double[,]存到xx.txt,怎样从xx.txt中读出这些数字到一个double[,]
导出代码如下:
1
string strOutput = string.Empty;
2
double[,] v = new double[3,4];
3
for(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
}
string strOutput = string.Empty;2
double[,] v = new double[3,4];3
for(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字符串就可以了,
输出方法:
1
FileInfo myFile = FileInfo(strFileName);//导出文件的路径
2
StreamWriter sw = myFile.CreateText(); //创建一个新文件
3
sw.Write(strOutput);
4
sw.Close();
5![]()
6
读入的代码:
7
StreamReader sr = new StreamReader(strFileName);
8
string strText = sr.ReadToEnd(); //把内容读到strText变量中去
9
sr.Close();
10
if(strText == null || strText.Trim() == string.Empty)
11
return;
12
string[] strRows = strText.Split('\n'); //分离出不同的行
13
if(strRows[0] == null || strRows[0].Trim() == string.Empty)//判断第一行是否有值
14
return;
15
string[] strCol = strRows[0].Split('\t');//分离第一行为不同的列
16
int rowCount = strRows.Length; //得到行数
17
int colCount = strCol.Length;//得到列数,虽然只是第一列的列数,但是string[,]中每行的列数是一定相同的
18
double[,] v = new double[rowCount, colCount];
19
for(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![]()
FileInfo myFile = FileInfo(strFileName);//导出文件的路径2
StreamWriter sw = myFile.CreateText(); //创建一个新文件3
sw.Write(strOutput);4
sw.Close();5

6
读入的代码:7
StreamReader sr = new StreamReader(strFileName);8
string strText = sr.ReadToEnd(); //把内容读到strText变量中去9
sr.Close();10
if(strText == null || strText.Trim() == string.Empty)11
return;12
string[] strRows = strText.Split('\n'); //分离出不同的行13
if(strRows[0] == null || strRows[0].Trim() == string.Empty)//判断第一行是否有值14
return;15
string[] strCol = strRows[0].Split('\t');//分离第一行为不同的列16
int rowCount = strRows.Length; //得到行数17
int colCount = strCol.Length;//得到列数,虽然只是第一列的列数,但是string[,]中每行的列数是一定相同的18
double[,] v = new double[rowCount, colCount];19
for(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[,]矩阵数组


浙公网安备 33010602011771号