如何将文本文件的第一行放在结束位置

How to put the first line of text file in the end position

我正在使用 visual studio 2012 C#。我有一个流作家。我将所有行写入文本文件,但我想将第一行放在文本文件的最后一个位置。写入流后,我想将第一行切换到最后一行,但第一行不应留空,而是将前面的行向上移动,然后再次写入流。我如何将第一行保存在变量中并从第二行开始编写代码的哪一部分。

这是我的代码

using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath))  
           {  
               string[] buffer = new string[count];  

               while (!(lines = reader.ReadLine()).Contains("Records"))  
               {  



                   for (int i = 0; i < count - 1; i++)  
                   {        
                       string[] fields = lines.Split('|');  


                       Result[i, 0] = "";  

                           Result[i, 1] = fields[1];                                 
                           Result[i, 2] = " ";  

                           Result[i, 3] = xx;  

                           Result[i, 4] = "";  
                           Result[i, 5] = fields[4];  
                           Result[i, 6] = "";  
                           Result[i, 7] = fields[0];                                
                           Result[i, 8] = "";  

                           if (fields[15].Contains("PASS"))  
                           {  
                               Result[i, 9] = "P";  
                           }  
                           else if (fields[15].Contains("FAIL"))  
                           {  
                               Result[i, 9] = "F";  
                           }                                                          
                           Result[i, 10] = "";  
                           Result[i, 11] = "";                                                   
                           Result[i, 12] = "";  
                           Result[i, 13] = "";  
                           Result[i, 14] = "";  

                           }  

                       }  



                       else if (fields.Length == 7)  
                       {  


                           Result[i, 1] = "";  

                           Result[i, 2] = "";  


                           Result[i, 3] = fields[0];                                                       //Test Code  
                           Result[i, 4] = "1";                                                                 // Test Channel  
                           Result[i, 5] =fields [4];                                                           // Test Value  
                           Result[i, 6] = "0";                                                             //Test Frequency  

                           if (fields[5].Contains("PASS"))   
                           {   
                               Result[i, 7] = "P";   
                           }  
                           else if (fields[5].Contains("FAIL"))   
                           {  
                               Result[i, 7] = "F";   
                           }   



                           Result[i, 8] = "0";                             // Test Volt



                                   Result[i, 9] = fields[1];  

                                   Result[i, 10] = "0.0";  

                               }  


                           }  

                           Result[i, 11] = "0";                       
                           Result[i, 12] = "1";                       
                           Result[i, 13] = "1";                      
                           Result[i, 14] = "0";                   
                           Result[i, 15] = Profile_Index.ToString();  

                       }        
                       result = ("PATS_TEST" + "," + Result[i, 1] + "," + Result[i, 2] + "," + Result[i, 3] + "," + Result[i, 4] + "," + Result[i, 5] + "," + Result[i, 6] + "," + Result[i, 7] + "," + Result[i, 8] + "," + Result[i, 9] + "," + Result[i, 10] + "," + Result[i, 11] + "," + Result[i, 12] + "," + Result[i, 13] + "," + Result[i, 14] + "," + Result[i, 15]);                                                                  
                       if (fields[0].Contains("END-OF-LINE"))  
                       {  
                           result = ("END-OF-LINE");  
                       }              
                   }             
                   Profile_Index++;             
                   // Console.WriteLine(result);        
                   file.WriteLine(result);                                                        
                       }

我通过这个 code.just 解决了我的问题。

 private void button_Click(object sender, EventArgs e)
        {
            string line = null;
            string FirstLineContent = "";
            int line_number = 0;
            int line_to_delete = 1;
            string tempFile = Path.GetTempFileName();
            using (StreamReader reader = new StreamReader(@"D:\Sample.txt"))
            {
                using (StreamWriter writer = new StreamWriter(tempFile))
                {
                    while ((line = reader.ReadLine()) != null)
                    {
                        line_number++;
                        if (line_number == line_to_delete)
                        {
                            FirstLineContent = line;
                        }
                        else
                        {
                            writer.WriteLine(line);
                        }
                    }
                    writer.WriteLine(FirstLineContent);
                }
            }
            File.Delete(@"D:\Sample.txt");
            File.Move(tempFile, @"D:\Sample.txt");
        }

你自己的回答看起来不错。但我试着让它更短,变量更少。

private void button3_Click(object sender, EventArgs e)
{
    //first we read all lines into a list
    List<string> allLines = new List<string>();
    using (var reader = new System.IO.StreamReader(@"D:\Sample.txt"))
        while (reader.Peek() >= 0)
            allLines.Add(reader.ReadLine());

    //if there are more than 1 lines...
    if (allLines.Count > 1)
    {
        //copy first line to end
        allLines.Add(allLines.First());
        //remove first line
        allLines.RemoveAt(0);
        //finally we write everything back in the same file
        using (var writer = new System.IO.StreamWriter(@"D:\Sample.txt"))
            foreach (var line in allLines)
                writer.WriteLine(line);
    }
}
  • 首先我们读取列表中的所有行。
  • 我们将第一行复制到列表的末尾。
  • 我们删除第一行。
  • 最后我们将所有内容写回同一个文件。