【问题标题】:Output the contents of an array of lists to a text file?将列表数组的内容输出到文本文件?
【发布时间】:2019-03-02 02:43:18
【问题描述】:

我目前有一个列表数组,如下所示:

List<string>[] phase2 = new List<string>[200];

里面的一些列表是用字符串初始化的(每个 8 个),如下所示:

phase2[0] = new List<string>() { "Bob", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes", };
phase2[1] = new List<string>() { "Joe", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[2] = new List<string>() { "Bill", "Complex", "A", "AOT", "Yes", "Yes", "Yes", "Yes" };

我正在尝试将每个列表的内容写入 .txt 文件并且遇到了我的知识块......我希望每个“列表”都在 1 行。我知道如何将数组写入文件和将列表写入文件,但不能同时写入。我希望得到一些帮助来解决这个问题。

【问题讨论】:

    标签: c# arrays list file-io text-files


    【解决方案1】:

    听起来您想使用“嵌套”foreach 循环,但不清楚您是希望它们写入两个单独的文件,还是希望它们写入同一个文件。我会做第二个:

    foreach (List<string> currentList in phase2)
    {
         foreach (string currentElement in currentList)
         {
              //change this out to whatever file output technique you use.               
              Console.Write currentElement + ",";
         }
       //starts a new line
       Console.Write "\n"
    }
    

    这应该让您知道去哪里(假设您的意思是要以列/行样式编写每个列表的内容)。我几乎坚持使用分隔符,除非你有特殊的理由不这样做。您可以使用任何字符,但坚持使用标准 (, ; | [tab] ) 来分隔列实际上只是确保与未来应用程序的兼容性。

    如果您不想要分隔符,则只需删除 + "," 部分。

    将 Console.Write 命令替换为您的文件输出内容。

    【讨论】:

      【解决方案2】:

      请参阅下面的“ConcatToString”函数:

      void Main()
      {
          List<string>[] phase2 = new List<string>[200];
      
          phase2[0] = new List<string>() { "Bob", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes", };
          phase2[1] = new List<string>() { "Joe", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes" };
          phase2[2] = new List<string>() { "Bill", "Complex", "A", "AOT", "Yes", "Yes", "Yes", "Yes" };
      
          File.WriteAllText(@"d:\scratch\arrOfWords.txt", ConcatToString(phase2));
      }
      
      public string ConcatToString(List<string>[] arrOfWords) 
      {
          StringBuilder sb = new StringBuilder();
          foreach(List<string> words in arrOfWords)
          {
              if(words?.Count > 0 ) sb.AppendLine(String.Join(",", words));
          }
          return sb.ToString();
      }
      

      【讨论】:

        【解决方案3】:

        正如@slightly-drifting 所指出的,在遍历二维数据结构时,您需要使用嵌套循环。它可以是foreach、传统的for,甚至是while,这取决于您的需要。

        但是,如果您不关心内存分配或性能(仅处理 200 行时似乎就是这种情况),您可以使用FileLINQ 来实现它。请注意,LINQ 提供了对用户更友好的 API,但是对于这个问题,在底层,它仍然使用嵌套循环。

        足够的单词。这是一个示例:

        private static void WriteToFile(string filePath, List<string>[] content)
        {
            using (var fileWriter = new StreamWriter(filePath))
            {
                foreach (var line in content)
                {
                    if (line == null)
                    {
                        continue;
                    }
        
                    for (var i = 0; i < line.Count; i++)
                    {
                        fileWriter.Write(line[i]);
        
                        if (i != line.Count - 1)
                        {
                            fileWriter.Write(',');
                        }
                    }
        
                    fileWriter.WriteLine();
                }
            }
        }
        
        private static void WriteToFileOneLiner(string filePath, List<string>[] content)
        {
            File.WriteAllLines(filePath, content.Where(line => line != null).Select(line => string.Join(',', line)));
        }
        
        public static void Main(string[] args)
        {
            List<string>[] phase2 = new List<string>[200];
            phase2[0] = new List<string>() { "Bob", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes", };
            phase2[1] = new List<string>() { "Joe", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes" };
            phase2[2] = new List<string>() { "Bill", "Complex", "A", "AOT", "Yes", "Yes", "Yes", "Yes" };
        
            WriteToFile(@"C:\Path\to\my\file.txt", phase2);
            WriteToFileOneLiner(@"C:\Path\to\my\file_oneliner.txt", phase2);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多