【问题标题】:How to Delete the last line of a file.txt如何删除file.txt的最后一行
【发布时间】:2013-02-19 23:00:12
【问题描述】:

我正在阅读 file.txt 使用 StreamReader 并使用 streamWriter 写作。

我想知道是否可以“只删除我创建的最后插入的行? i'm inserting lines, but sometimes one of these inserts arestring.empty. Then It goes to anexception”...在这个异常中,我想删除我刚刚插入的那一行.

但是我重新创建文件或类似的东西,我只需要删除/擦除/删除最后一行。我可以这样做吗?

MyCode:如果你们有其他方法可以做到这一点,我将非常感谢!

使用 (StreamWriter streamW = new StreamWriter(fileFinal, true)) { if (contador == numeroCampos) { 康塔多 = 0; }

  foreach (string s in clb_frente_impressao.Items) 
    {                            
    if (camposEscritos >= 10 * numeroCampos) 
      {
        streamW.WriteLine();
        streamW.WriteLine("-------------------------------------------------------");
        streamW.Write(nomearquivo + x.ToString());
        streamW.WriteLine();
        x++;
        camposEscritos = 0;
       }

       if (contador >= clb_frente_impressao.Items.Count)
          {
            contador = 0;
          }
       switch (s)
         {
            case "numero_carteira":
            if (campos_obrigatorios.Contains("numero_carteira") && campo.numero_carteira == "")
               {
                 dados_inexistentes++;
                 skipToNext = true;
                 break;
                }
                else if (campo.numero_carteira != "")
                   {
                      string aux = "";
                      qtdZeros = Txt_qtdZeros.Text;
                      if (qtdZeros == "")
                        {
                           qtdZeros = "8";
                        }
                   while (campo.numero_carteira.Length < Convert.ToInt32(qtdZeros))
                       {
                         campo.numero_carteira = campo.numero_carteira.Insert(0, "0");
                       }

                    if (usaC == "sim")
                        {
                      campos += @"\" + "*C" + aux + campo.numero_carteira + "*" + @"\";
                                                camposEscritos++;
                         }
                                            else
                                            {
                                                campos += @"\" + "*" + aux + campo.numero_carteira + "*" + @"\";
                                                camposEscritos++;
                                            }

                                            if (contador == 0)
                                            {
                                                streamW.WriteLine();
                                                streamW.Write("{0,-15}", campo.numero_carteira);
                                                contador++;
                                            }
                                            else
                                            {
                                                streamW.Write("{0,-15}", campo.numero_carteira);
                                                contador++;
                                            }
                                        }
                                       break;

                                    case "matricula":
                                       if (campos_obrigatorios.Contains("matricula") && campo.matricula == "")
                                       {
                                           dados_inexistentes++;
                                           skipToNext = true;
                                           break;
                                       }
                                        camposEscritos++;
                                        if (campo.matricula != "")
                                        {
                                            if (campo.tipo_pessoa == "3")
                                            {
                                                campos += @"\" + campo.matricula + "-" + campo.cod_dependente + @"\"; 
                                            }
                                            else
                                            {
                                                campos += @"\" + campo.matricula + @"\"; 
                                            }
                                        }
                                        if (contador > 0)
                                        {
                                            if (campo.cod_dependente != "")
                                            {
                                                streamW.Write("{0,-10}", campo.matricula + "-" + campo.cod_dependente);                                            
                                                contador++;
                                            }
                                            else
                                            {                                            
                                                streamW.Write("{0,-10}", campo.matricula);
                                                contador++;
                                            }
                                        }
                                        else
                                        {
                                            if (campo.cod_dependente != "")
                                            {
                                                streamW.WriteLine();
                                                streamW.Write("{0,-10}", campo.matricula + "-" + campo.cod_dependente);
                                                contador++;
                                            }
                                            else
                                            {
                                                streamW.WriteLine();
                                                streamW.Write("{0,-10}", campo.matricula);
                                                contador++;
                                            }
                                        }
                                        break;
  if (skipToNext) break;

                        } //Final do ForEach

                        if (skipToNext)
                        {
                            //HERE IS WHERE I WANT TO DELETE THE LAST LINE THAT WAS WRITED BY streamW
                            continue;
                        }

例如:当它进入case:"numero_carteira" 并且它不为空时,它会写入正常,但是当我到达matricula 并且它为空时,它将break 并转到我创建的异常。我想删除那里的行;希望我能清楚!

【问题讨论】:

  • 请清理你的代码格式。阅读/滚动浏览它时我感到头晕目眩。
  • @DJKRAZE 对不起,我不明白,真的。我与Ctrl + K 组成了我还能做什么?
  • 为什么不使用 stringBuilder 来生成内容,然后只写入文件一次?
  • 你的case语句也在寻找一个字符串为什么不在case "":做一个case

标签: c# winforms visual-studio-2010


【解决方案1】:

如果文件不是太大,你可以简单地使用这个代码:

var lines = File.ReadAllLines(pathToFile);
File.WriteAllLines(pathToFile, lines.Take(lines.Length - 1));

所以你必须写除最后一行之外的所有行。

【讨论】:

  • 这个方法到底能做什么?我问这个是因为我无法覆盖文件或重新创建一个文件哦,我猜它首先读取整个文件并写入所有已读取的内容,但最后一行,这就是为什么 lengh -1 对吗?
  • @Ghaleon:是的。为什么不能覆盖文件?当你想“删除”最后一行时,这就是你基本上正在做的事情。
  • @TimSchmelter 还记得我上一篇你帮助过我的帖子吗?好吧,我在 file.txt 上创建了类似 TABLE 的东西,所以不能删除列的名称,这就是为什么我只需要删除最后一行,或者用另一种方法来完成所有这些......我'我对此感到疯狂;x
  • 工作完美!谢谢@TimSchmelter
  • @TimSchmelter 但有些东西我们必须改变...当执行代码File.WriteAllLines(pathToFile, lines.Take(lines.Length - 1)); 它会正确擦除行,但留下blank line,我不想要它;s 有没有解决方法?
猜你喜欢
  • 2010-11-03
  • 2019-11-11
  • 2018-06-29
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 2013-12-19
相关资源
最近更新 更多