Problem Reading Text File with Streamreader

Asked

Viewed 154 times

0

I have an application that has a form that when loaded performs the reading of a text file, from the first line to the last, with StreamReader. The problem is I can’t read this same file twice in a row. The only way to read the file again is to close the entire system and start again. In the first reading, at the end I execute the closure of the instance. Ex. Instancia.Close(). But doing a debugging check that before running the Close method the instance is with the message:

EndOfStream = 'srv.EndOfStream' gerou uma exceção do tipo System.ObjectDisposedException'.

I keep running it, and I close this form. When I load the form again, for the second consecutive time, when will run the While the instance is already as the same error and the While is closed indicating that already arrived at the end of the archive.

As can be the end of the file, if theoretically it was closed at the end of the first execution and when I load again the reading should start from the beginning without major problems.

Can you please give me an idea of what’s going on?.

Thank you.

Follows the code:

namespace TesteStreamReaderACBRSAT
{
    public partial class LeituraArquivo : Form
    {
        static string linha;

        static string ArquivoVendaIni = "C:\\SAT\\VENDA.INI";
        static string ArquivoEntrada = @"C:\SAT\ENT.TXT";

        static StreamReader srv = new StreamReader(ArquivoVendaIni);
        static StreamWriter swe = new StreamWriter(ArquivoEntrada, true);

        public LeituraArquivo()
        {
            InitializeComponent();
        }

        private void LeituraArquivo_Load(object sender, EventArgs e)
        {
            LerArquivo();
        }

        public static void LerArquivo()
        {
            if (File.Exists(ArquivoVendaIni))
            {
                try
                {
                    while ((linha = srv.ReadLine()) != null)
                    {
                        VerQualEmpresa();

                        GravarArquivo();
                    }

                    swe.Close();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                }
            }
            else
            {
                //MessageBox.Show(" O arquivo " + arquivo + " não foi localizado");
            }
        }

        private static void GravarArquivo()
        {
            swe.WriteLine(linha);
        }

        private static void VerQualEmpresa()
        {
            if (true)  // Aqui se uma determinada condição for verdadeira deve-se ler algumas
                       // linhas do arquivo e depois a leitura continua no método LeituraArquivo()
            {
                while ((linha = srv.ReadLine()) != null)
                {
                    GravarArquivo();
                }
            }
        }
    }
}
  • 1

    Without code it’s hard to understand what you might be doing wrong

  • 1

    This may be related to how you are opening and closing Streamreader. The best thing to do is to put in the code you’re using.

  • I agree with colleagues. I will arrange the display of the code.

1 answer

-1

Not sure what the purpose of the method is VerQualEmpresa, I think the most correct code would be:

namespace TesteStreamReaderACBRSAT
{
    public partial class LeituraArquivo : Form
    {
        string ArquivoVendaIni = @"C:\SAT\VENDA.INI";
        string ArquivoEntrada = @"C:\SAT\ENT.TXT";

        public LeituraArquivo()
        {
            InitializeComponent();
        }

        private void LeituraArquivo_Load(object sender, EventArgs e)
        {
            LerArquivo();
        }

        public void LerArquivo()
        {
            try
            {
                if (File.Exists(ArquivoVendaIni))
                {
                    using(StreamReader sr = new StreamReader(ArquivoVendaIni))
                    {
                        using(StreamWriter sw = new StreamWriter(ArquivoEntrada, true))
                        {
                            VerQualEmpresa(sr, sw);

                            while (sr.Peek() >= 0) 
                                GravarArquivo(sw, sr.ReadLine());
                        }

                        sw.Close();
                    }
                }
                else MessageBox.Show(" O arquivo " + arquivo + " não foi localizado");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void GravarArquivo(StreamWriter sw, string linha)
        {
            sw.WriteLine(linha);
        }

        private void VerQualEmpresa(StreamReader sr, StreamWriter sw)
        {
            // Aqui se uma determinada condição for verdadeira deve-se ler algumas
            // linhas do arquivo e depois a leitura continua no método LeituraArquivo()
            if (true)  
            {
                while (sr.Peek() >= 0) 
                    GravarArquivo(sw, sr.ReadLine());
            }
        }
    }
}

Of course, if it remains as it is, after the execution of the method VerQualEmpresa the StreamReader has come to an end and no more lines will be read.
Perhaps you should validate what the code should actually do...

Browser other questions tagged

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