无法从关闭的 TextReader 中读取 [英] Cannot read from a closed TextReader

查看:66
本文介绍了无法从关闭的 TextReader 中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理一些大型 CSV 文件的系统.

I've got a system that processes some large CSV files.

现在出现的情况是,这些文件可能在实际逗号分隔内容之前有许多无分隔、毫无价值的行.

The scenario has now arisen where these files might have a number of none-delimited, worthless lines preceding the actual comma-delimited content.

我采用的方法是创建一个临时读取器来确定多余行的数量,然后将工作 TextReader 移动到准备处理的行数上.

The approach I have taken is to create a temporary reader to ascertain the number of superfluous lines and then move the working TextReader on that number of lines ready to be processed.

我的代码如下:

private static TextReader PrepareReader(TextReader reader)
    {
        // Variables
        TextReader tmpReader = reader;
        Int32 superfluousLineCount = 0;

        // Determine how many useless lines we have
        using (tmpReader)
        {
            string line;
            string headerIdentifier = "&1,";
            while ((line = tmpReader.ReadLine()) != null)
            {
                // Check if the line starts with the header row identifier
                if (line.Substring(0, 3) != headerIdentifier)
                {
                    // Increment the superfluous line counter
                    superfluousLineCount++;
                }
                else
                {
                    break;
                }
            }
        }

        // Move the source reader through how many lines we want to ignore
        using (reader)
        {
            for (int i = superfluousLineCount; i > 0; i--)
            {
                reader.ReadLine();
            }
        }

        // Return
        return reader;
    }

但是,这部分代码中的reader.ReadLine();:

However, the reader.ReadLine(); in this part of the code:

for (int i = superfluousLineCount; i > 0; i--)
{
reader.ReadLine();
}

...抛出以下异常

无法从关闭的 TextReader 中读取.mscorlib 中的 ObjectDisposedException方法:Void ReaderClosed()

Cannot read from a closed TextReader. ObjectDisposedException in mscorlib Method: Void ReaderClosed()

堆栈跟踪:在 System.IO.__Error.ReaderClosed()在 System.IO.StreamReader.ReadLine()在 CsvReader.cs:line 93

Stack Trace: at System.IO.__Error.ReaderClosed() at System.IO.StreamReader.ReadLine() at CsvReader.PrepareReader(TextReader reader) in CsvReader.cs:line 93

非常感谢任何建议.另外,这是应对挑战的最佳方式吗?

Any advice greatly appreciated. Also, is the best way to go about my challenge?

注释:框架 2.0

谢谢.

推荐答案

当你使用 using (tmpReader) 它会关闭 tmpReaderreader 是),所以当你尝试从循环中的 reader 读取时,它是关闭的.

When you are using using (tmpReader) it will close tmpReader (Which references the same object as reader does), so when you try to read from reader in your loop, it is closed.

最好的办法是结合这两个循环.既然你只想跳过行,我认为第一个循环的逻辑就足够了.

Your best bet is to combine the two loops. Sine you only want to skip lines, I would think the logic of the first loop is sufficient.

这篇关于无法从关闭的 TextReader 中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆