C # Read TXT file and delete

2
Good afternoon, I'm developing a program that reads a certain file (contains more than 60k of 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();
    }
}

But in return it only returns me the 1st column of the file and I wanted the return of the entire line

    
asked by anonymous 20.12.2018 / 18:27

2 answers

2

So you can not 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 do not need sw.Close() because using already does this (and Dispose of the object).

    
20.12.2018 / 19:01
2

Switch:

sw.WriteLine(linha[1]);

To:

sw.WriteLine(item.Linha);
    
21.12.2018 / 11:13