C# 读入和输出文本数据
读入数据
1.读取一列数据,并储存到一维浮点数组中
public static double[] readFromFile(string path)
{
if (File.Exists(path))
{
string[] s = File.ReadAllLines(path);
double[] data = new double[s.Length];
for (int i = 0; i < s.Length; i++)
{
data[i] = double.Parse(s[i]);
}
return data;
}
else
{
Console.WriteLine("未找到文件: {0}", path);
return null;
}
}
2.读取表格数据,储存到交错数组(浮点)中
public static double[][] readFromFile(string path, int passRowNum, int passColNum, string split)
{
if (File.Exists(path))
{
string[] st = File.ReadAllLines(path);
double[][] data = new double[st.Length - passRowNum][];
for (int i = passRowNum; i < st.Length; i++)
{
string[] sta = Regex.Split(st[i].Trim(), split);
data[i - passRowNum] = new double[sta.Length - passColNum];
for (int j = passColNum; j < sta.Length; j++)
{ data[i - passRowNum][j - passColNum] = double.Parse(sta[j]); }
}
return data;
}
else { Console.WriteLine("未找到文件:" + path); return null; }
}
3.读取表格数据,按列储存到交错数组中
public static double[][] readFromFile_column(string path, int passRowNum, int passColNum, string split)
{
if (File.Exists(path))
{
string[] FileContent = File.ReadAllLines(path);
int rowNum_all = FileContent.Length;
int rowNum = rowNum_all - passRowNum;
int columnNum_all = Regex.Split(FileContent[passRowNum].Trim(), split).Length;
int columnNum = columnNum_all - passColNum;
double[][] result = new double[columnNum][];
for (int i = 0; i < columnNum; i++) { result[i] = new double[rowNum]; }
for (int i = passRowNum; i < rowNum_all; i++)
{
string[] content = Regex.Split(FileContent[i].Trim(), split);
for (int j = passColNum; j < columnNum_all; j++)
{
bool tryParse = double.TryParse(content[j], out result[j - passColNum][i - passRowNum]);
if (tryParse == false) result[j][i - passRowNum] = double.NaN;
}
}
return result;
}
else
{
Console.WriteLine("未找到文件: {0}", path);
return null;
}
}
4.读取表格数据,储存到交错数组(字符)中
public static string[][] readFromFile(string path, string split)
{
string[][] n;
if (File.Exists(path))
{
string[] st = File.ReadAllLines(path);
n = new string[st.Length][];
for (int i = 0; i < st.Length; i++)
{
n[i] = Regex.Split(st[i].Trim(), split);
}
}
else { Console.WriteLine("未找到文件:" + path);n = null; }
return n;
}
输出数据
1.输出一维数组
public static void outputData<T>(T[] data,string outputFile)
{
StreamWriter wr = new StreamWriter(outputFile);
for(int i = 0; i < data.Length; i++)
{
wr.WriteLine(data[i]);
}
wr.Close();
}
2.按行输出二维数组
public static void outputData<T>(T[][] data, string split, string outputFile)
{
StreamWriter wr = new StreamWriter(outputFile);
for (int i = 0; i < data.Length; i++)
{
for (int j = 0; j < data[i].Length; j++)
{
wr.Write(data[i][j].ToString() + split);
}
wr.Write("\n");
}
wr.Close();
}
3.按列输出二维数组
public static void outputData_column<T>(T[][] data, string outputFile, string split)
{
StreamWriter wr = new StreamWriter(outputFile);
int cols = data.Length;
int rows = data[0].Length;
for(int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
wr.Write(data[j][i].ToString() + split);
}
wr.Write("\n");
}
wr.Close();
}