2016-11-09 69 views
0

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

回答

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"); 
    } 
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); 
    } 
} 
  • 首先我們先讀列表中的所有行。
  • 我們將第一行復制到列表的末尾。
  • 我們刪除第一行。
  • 最後我們把所有的東西寫回到同一個文件中。
+0

它也可以是答案。感謝您的答覆。 –

+1

@NaveenSoni不客氣。 –

相關問題