c# - txt 文件读取/覆盖/追加。这可行吗? (视觉 C#)

标签 c#

我正在为我必须定期进行的一些数据输入编写一个程序。我已经开始测试该程序必须做的一些事情,但我不确定这部分。

我需要这部分做的是:

读取 .txt 数据文件

取每行的前12个字符

从多行文本框中输入的每行数据中取出前12个字符

逐行比较两个列表

如果多行文本框中的 12 个字符 block 之一与 .txt 文件中的 block 之一匹配,则覆盖整行(总共只有 17 个字符)

如果多行文本框中的 12 个字符 block 之一与 .txt 文件中的任何 block 不匹配,则将整行附加到文件中

这就是它所要做的。

我举个例子:

TXT 文件:

G01:78:08:32 JG05
G08:80:93:10 JG02
G28:58:29:28 JG04

多行文本框:

G01:78:08:32 JG06
G28:58:29:28 JG03
G32:10:18:14 JG01
G32:18:50:78 JG07

生成的 TXT 文件:

G01:78:08:32 JG06
G08:80:93:10 JG02
G28:58:29:28 JG03
G32:10:18:14 JG01
G32:18:50:78 JG07

如您所见,第 1 行和第 3 行被覆盖,第 2 行被保留,因为它与文本框中的任何 block 都不匹配,第 4 行和第 5 行被附加到文件中。

这就是我想要它做的一切。

我该怎么做?

提前致谢

编辑

我使用的代码是这样的:

        private void WriteToFile()
    {
         // Read all lines from file into memory
        System.IO.StreamReader objReader = new System.IO.StreamReader("Jumpgate List.JG");
        List<String> fileTextList = new List<String>();
                 do
                     {
                         fileTextList.Add(objReader.ReadLine());
                     }
                 while (objReader.Peek() != -1);

                 objReader.Close();

         // Read all lines from the Input textbox into memory
             System.IO.StringReader objReaderi = new System.IO.StringReader(txtInput.Text);
             List<String> inputTextList = new List<String>();
                 do
                     {
                         inputTextList.Add(objReaderi.ReadLine());
                     } 
                 while (objReaderi.Peek() != -1);

                 objReaderi.Close();

         for(int i=0;i<fileTextList.Count;i++)
             {
                 for(int j=0;j<inputTextList.Count;j++)
                  //compare the first 12 characters of each string
                  if (String.Compare(fileTextList[i], 0, inputTextList[j], 0, 12) == 0) // strings are equal
                     {
                       //replace the fileTextList string with the inputTextList string
                         fileTextList[i] = inputTextList[j];
                       // now that you have matched you inputTextList line you remember not to append it at the end
                        inputTextList[j] = String.Empty; // or nothing
                     }
             }

          for(int i=0;i<inputTextList.Count;i++)
             {
                 if (!string.IsNullOrEmpty(inputTextList[i])) fileTextList.Add(inputTextList[i]);
              }

          System.IO.StreamWriter objWriter = new System.IO.StreamWriter("Jumpgate List.JG");

          // Overwrite the Jumpgate List.JG file using the updated fileTextList
          objWriter.Write(fileTextList);

          objWriter.Close();

但是,当我打开 txt 文件时,我得到的是:System.Collections.Generic.List`1[System.String]

最佳答案

我不会为此编写完整的代码,但它会是这样的:

免责声明:我没有使用代码编辑器来尝试代码,只是写在这里,希望你能理解并填补缺失的部分:)

1) 获取列表中文件中的所有行。像这样

        StreamReader rd = new StreamReader("sadasd");
        List<String> llist = new List<String>();
            do
            {
                llist.Add(rd.ReadLine());

            } while (rd.Peek() != -1);

2) 获取多行文本框中的所有行(过程应该与上述类似):multiTextList

3) 现在您可以比较遍历它们的 2 个列表的内容

for(int i=0;i<fileTextList.Count;i++)
{
    for(int j=0;j<multiTextList.Count;j++)
     //compare the first 12 characters of each string
     if String.Compare(fileTextList[i], 0, multiTextList[j], 0, 12) == 0 // strings are equal
{
      //replace the initial line with whatever you want
       fileTextList[i] = //whatever
      // now that you have matched you multiTextList line you remember not to append it at the end
       multiTextList[j] = String.empty // or nothing
}
}

4) 最后,您将在 fileTextList 中拥有初始行,必要时进行修改 在 multiTextList 中,您将只有不匹配的行,因此我们将它们添加到初始文件行中

      for(int i=0;i<multiTextList.Count;i++)
        {
if !string.isnullorempty(multitextlist[i]) fileTextList.add(multitextlist[i])
         }

5) 现在在 fileTextList 中你有你需要的所有行所以你可以在一个文件中一个一个地打印它们并且你有你的结果

 StringBuilder lSb = new StringBuilder();
            for (int i = 0; i < fileTextList.Count; i++)
            {
                lSb.AppendLine(fileTextList[i]);
            }
            File.WriteAllText(@"C:/test2.txt",lSb.ToString());

C:/test2.txt 中你应该有结果。

希望这对您有所帮助!

关于c# - txt 文件读取/覆盖/追加。这可行吗? (视觉 C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2991446/

相关文章:

c# - 类在系统中有哪些类型的关联?如何在 UML 中最好地表示它们?

c# - 远程桌面连接 - C# 事件

c# - 使用 ShouldBeEquivalentTo 时如何排除 IEnumerable 中所有项目的属性?

C# 变量初始化与赋值

c# - xunit 构造函数在每次测试之前运行

c# - 使用LINQ获取两个表之间的差异

c# - 将字典绑定(bind)到中继器

c# - Python 中使用十六进制进行位移位

c# - 列表框显示两个时间相同的值

c# - 这是在编辑模式下使用 GridView 格式的错误还是复杂的编码解决方案?