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

Posted

技术标签:

【中文标题】如何将文本文件的第一行放在结束位置【英文标题】:How to put the first line of text file in the end position 【发布时间】:2016-11-09 09:41:54 【问题描述】:

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

【问题讨论】:

【参考方案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);
    

首先我们读取列表中的所有行。 我们将第一行复制到列表的末尾。 我们删除第一行。 最后我们将所有内容写回到同一个文件中。

【讨论】:

它也可以是一个答案。感谢您的回复。【参考方案2】:

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

 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");
        

【讨论】:

以上是关于如何将文本文件的第一行放在结束位置的主要内容,如果未能解决你的问题,请参考以下文章

将文本添加到文件的第一行 [重复]

如何拆分文本文件并在一行中存储 2 个值?

使用 Tomcat 将文本文件放在 Eclipse 动态 Web 项目中的啥位置?

C# 文件流 streamreader如何读取文本指定行的数据?

JavaFX:TextArea 光标移回新文本的第一行

python读取指定目录中所有文本文件的第一行,并以此为该文本文件名重命名