C# Read TXT file and delete

Asked

Viewed 498 times

2

Good Afternoon, I am developing a program that reads a certain file (contains more than 60k lines) removes unnecessary use and organizes and writes to another file.

using (StreamReader leitor = new StreamReader(lerArquivo))
{
    while (!leitor.EndOfStream)
    {
        var linha = new ArquivoTxt();
        linha.Linha = leitor.ReadLine();
        conteudoArquivo.Add(linha);
    }

    var gravarArquivo = @"C:\log\meutxt.txt";

    using (StreamWriter sw = new StreamWriter(gravarArquivo))
    {
        conteudoArquivo = conteudoArquivo.Where(_ => _.Linha.StartsWith("|") &&
                                                     _.Linha.Substring(0, 2) != "|0" &&
                                                     _.Linha.Substring(0, 2) != "|-" &&
                                                     _.Linha.Substring(0, 2) != "|C" &&
                                                     _.Linha.Substring(0, 2) != "|I" &&
                                                     _.Linha.Substring(0, 2) != "| " &&
                                                     _.Linha.Substring(0, 2) != "|R" &&
                                                     _.Linha.Substring(0, 2) != "|D" &&
                                                     _.Linha.Substring(0, 2) != "|E").ToList();

        foreach (var item in conteudoArquivo)
        {
            string[] linha = item.Linha.Split('|');
            string ordemVenda = linha[1].Trim();
            sw.WriteLine(linha[1]);
        }

        sw.Close();
    }
}

only that in return it only returns me the 1st column of the File and I wanted the return of the whole Line

  • 1

    it’s not because you’re just reading the linha[1] ??

  • 1

    And why do you use the using and closes the file? And there is some reason to create variable where you do not need and stop creating where will be very useful?

  • @Leandroangelo I’ve tried to change does not return what I want

  • @Maniero I’m starting, I don’t know much yet.

2 answers

2


So you don’t get what you want?

string[] caracteresAceites = new string[] { "|0", "|-", "|C", "|I", "| ", "|R", "|D", "|E" };

using (StreamReader leitor = new StreamReader(lerArquivo))
{
    using (StreamWriter sw = new StreamWriter(@"C:\log\meutxt.txt"))
    {
        while (!leitor.EndOfStream)
        {
            string linha = leitor.ReadLine();

            if(linha.StartsWith("|") && !caracteresAceites.Contains(linha.Substring(0, 2)))
            {
                string[] _linha = linha.Split('|');

                if(_linha.Length > 1)
                    sw.WriteLine(_linha[1].Trim());
            }
        }
    }
}

I gave an "optimized" in your code

You don’t need the sw.Close() because the using already does that (and the Dispose of the object).

  • That beautiful optimized in :D, I’m starting in programming, thanks for the great optimized and clear leaving the code easier to understand

  • Doesn’t solve your problem? Thank you :)

  • I could not, in case he returns me only the 1 column Order of Sale, I wanted him to return me the row of the whole notepad, but he is breaking the line and cleaning everything that comes after the |Order of Sale|

2

Change:

sw.WriteLine(linha[1]);

for:

sw.WriteLine(item.Linha);
  • boy! thanks I traded (line[1]) for (item.Line), and got what I expected.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.