1、filepath是文件路径
public void JXFile(string filepath)
{
//解析文件
string filename = filepath;
m_filename = Path.GetFileNameWithoutExtension(filename);
string newfile = OutTxt(m_filename);
int lastIndex = newfile.LastIndexOf('\\');
if (lastIndex != -1)
{
string result = newfile.Substring(lastIndex + 1);
Console.WriteLine(result); //
}
using (StreamReader reader = new StreamReader(filename))
{
// 跳过第一行
reader.ReadLine();
// 从第二行开始读取
string line;
while ((line = reader.ReadLine()) != null)
{
string[] columns = line.Split(' '); // 假设列是用逗号分隔的
Console.WriteLine(line);
if (!line.Contains("特殊标记 "))
{
foreach (string column in columns)
{
Console.Write($"{column.Trim()} "); // 输出每列的值并去除前后空白
if (!string.IsNullOrEmpty(column))
{
decimal dems = ChangeValue(column.Trim()); //转换函数
File.AppendAllText(newfile, dems.ToString() + " ");
}
}
File.AppendAllText(newfile, "\r");
}
}
}
}
2、调用对应函数
private double ChangeValue(string strvalue)
{
double dData = 0.0f;
if (strData.Contains("E"))
{
dData = double.Parse(strvalue.ToString(), System.Globalization.NumberStyles.Any);
}
else
{
if (!strData.Contains("I")) //对其中特殊符号进行处理比如|
{
dData = double.Parse(strvalue);
}
}
return Math.Round(dData, 5);
}
3、生成新的txt,filename是具体文件名称
private string OutTxt(string filename)
{
string str = string.Empty;
//输出txt
if (!File.Exists(Application.StartupPath + "\\结果\\"+filename+".txt"))
{
FileStream myfs = new FileStream(Application.StartupPath + "\\结果\\" + filename+".txt", FileMode.Create, FileAccess.ReadWrite);
str= Application.StartupPath + "\\结果\\" + filename + ".txt";
myfs.Close();
}
return str;
//是否打开当前txt
Process.Start(Application.StartupPath + "\\结果\\" + filename+".txt");
}