如何删除file.txt的最后一行 [英] How to Delete the last line of a file.txt
问题描述
我正在使用 StreamReader
读取 file.txt
并使用 streamWriter
编写.
I'm reading a file.txt
using StreamReader
and writing using streamWriter
.
我想知道是否可以仅删除最后插入的行<代码>?我正在插入行,但有时这些插入之一是string.empty.然后它转到我创建的
异常`...在这个异常中,我想删除我刚刚插入的那一行.
I'd like to know if it's possible to ´DELETE JUST THE LAST INSERTED LINE?
i'm inserting lines, but sometimes one of these inserts are
string.empty. Then It goes to an
exception`that I created... In this exception, I want to delete that line I just inserted.
但我重新创建文件,或类似的东西,我只需要删除/擦除/删除最后一行.我可以这样做吗?
But I recreate the file, or something like that, I need just to remove/erase/delete the last line. May I do that ?
MyCode:如果你们有其他方法可以做到这一点,我将非常感激!
MyCode: IF you guys have another way to do this, i'd be very thankfull !
使用 (StreamWriter streamW = new StreamWriter(fileFinal, true)){如果(contador == numeroCampos){交易者 = 0;}
using (StreamWriter streamW = new StreamWriter(fileFinal, true)) { if (contador == numeroCampos) { contador = 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
并转到我创建的异常.我想删除那里的那一行;希望我能说清楚!
e.g: When It gets into case:"numero_carteira"
and it's not empty,then it writes ok, but when I get to the matricula
AND its empty, it will break
and go to the exception I created. I want to delete the line there ;s Hope I could be clear !
推荐答案
如果文件不是太大,可以简单地使用这个代码:
If the file is not too large, you can simply use this code:
var lines = File.ReadAllLines(pathToFile);
File.WriteAllLines(pathToFile, lines.Take(lines.Length - 1));
所以你必须写除最后一行之外的所有行.
So you have to write all lines except the last.
这篇关于如何删除file.txt的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!