如何将第一行文本文件放在最终位置

时间:2016-11-09 09:41:54

标签: c# asp.net streamwriter

我正在使用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);                                                        
                       }

2 个答案:

答案 0 :(得分:1)

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

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);
    }
}
  • 首先,我们阅读列表中的所有行。
  • 我们将第一行复制到列表的末尾。
  • 我们删除第一行。
  • 最后我们将所有内容写回同一个文件中。

答案 1 :(得分:0)

我通过这段代码解决了我的问题。看到这个。

private void button3_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");
    }