111,129
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
namespace Demotest1
{
class Program
{
static void Main(string[] args)
{
//基本路径
string path = "d:\\我的文档\\桌面\\csv\\";
//要读取的文件名称
string[] name = new string[15]{
"Armor Channel - 20100908-085305.csv",
"Armor Channel - 20100908-085355.csv",
"Armor Channel - 20100908-085454.csv",
"Armor Channel - 20100908-085555.csv",
"Armor Channel - 20100908-085654.csv",
"Armor Channel - 20100908-085755.csv",
"Armor Channel - 20100908-085857.csv",
"Armor Channel - 20100908-085955.csv",
"Armor Channel - 20100908-090054.csv",
"Armor Channel - 20100908-090155.csv",
"Armor Channel - 20100908-090255.csv",
"Armor Channel - 20100908-090355.csv",
"Armor Channel - 20100908-090455.csv",
"Armor Channel - 20100908-090555.csv",
"Armor Channel - 20100908-090654.csv",
};
//求平均值的个数
int len = 0;
//存文件数据
ArrayList list = new ArrayList();
//循环文件数
for (int i = 0; i < name.Length; i++)
{
//读取文件
string fullPath = Path.Combine(path, name[i]);
string txt = "";
StreamReader sr = new StreamReader(fullPath);
while (!sr.EndOfStream)
{
string str = sr.ReadLine();
txt += str + "\n";
}
sr.Close();
//按日期分割
string[] f = Regex.Split(txt, "20100908", RegexOptions.IgnoreCase);
//将第四个分割字符串按";"分割
string[] temp = f[3].Split(';');
float[] f1 = new float[temp.Length];
for (int k = 2; k < f1.Length; k++)
{
f1[k] = Convert.ToSingle(temp[k]);
}
//求平均值的个数
len = f1.Length;
//将数组添加到集合
list.Add(f1);
}
//最后得到的结果
String newfileStr = "";
for (int i = 2; i<len ; i++)//加多少次256
{
// 总和
float count = 0;
// i=0;j=1 每个文件加多少次
for (int j = 0; j<list.Count; j++)
{
//读取第j个数组
float [] f = (float [])list[j];
//读取第j个数组的第i个数字,然后求和
count = Convert.ToSingle(count + f[i]);
}
//将总和除以文件数,在存到newfileStr里
if (newfileStr == "")
{
newfileStr =Convert.ToString( count / list.Count);
}
else {
newfileStr = newfileStr + ";" + count / list.Count;
}
}
Console.WriteLine(newfileStr);
StreamWriter sw = File.CreateText("d:\\我的文档\\桌面\\csv\\myText.dx");
sw.WriteLine(newfileStr);
sw.Flush();
sw.Close();
}
}
}