[英]Save Array to text in C#
I've read a file from a text file and parsed the data I want, but I don't know how to save the parsed data in form to the file.我已经从文本文件中读取了一个文件并解析了我想要的数据,但我不知道如何将解析后的数据以表格的形式保存到文件中。 Are you able to assist me?你能帮助我吗? I've also included a snippet from the file.我还包含了文件中的一个片段。 https://ibb.co/G5hTtky --> link for the image https://ibb.co/G5hTtky --> 图片链接
public static void Main(string[] args)
{
StreamReader streamReader = File.OpenText(@"C:\Users\asr050322.txt");
string text = streamReader.ReadToEnd();
string[] linesArray = text.Split("BOH");
for (int i = 0; i < linesArray.Length; i++)
{
if (linesArray[i].Substring(24, 5) == "UXOAP")
{
File.WriteAllLines(@"C:\Users\HeUXOAP050322.txt", Array.ConvertAll(linesArray[i], x => x.ToString()));
}
if (linesArray[i].Substring(24, 5) == "UXOGS")
{
File.WriteAllLines(@"C:\Users\UXOGS050322.txt", Array.ConvertAll(linesArray[i], x => x.ToString()));
}
}
}
File.WriteAllLines()
will overwrite an existing file, so as you loop, you will only keep the latest data. File.WriteAllLines()
将覆盖现有文件,因此当您循环时,您只会保留最新数据。
Use File.AppendText("path/to/file", linesArray[i]);
使用File.AppendText("path/to/file", linesArray[i]);
This will create a file if it does not exist, then appends the data to the end of the existing file.如果文件不存在,这将创建一个文件,然后将数据附加到现有文件的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.