如何读取文本文件中的特定部分? [英] How to read a specific part in text file?

查看:114
本文介绍了如何读取文本文件中的特定部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的文本文件(500MB),我需要得到它的文本。
套餐的问题是异常的内存不足,但我想以字符串(或字符数组)来解决它,并把他们在列表中。
我在谷歌搜索,我真的不知道如何采取的特定部分。
*这是一个长行,如果这能帮助

I have a really big text file (500mb) and i need to get its text. Of course the problem is the exception-out of memory, but i want to solve it with taking strings (or char arrays) and put them in List. I search in google and i really don't know how to take a specific part. * It's a one long line, if that helps.

推荐答案

做的是:

using (FileStream fsSource = new FileStream(pathSource,
        FileMode.Open, FileAccess.Read))
    {

        // Read the source file into a byte array.
        int numBytesToRead = // Your amount to read at a time
        byte[] bytes = new byte[numBytesToRead];

        int numBytesRead = 0;
        while (numBytesToRead > 0)
        {
            // Read may return anything from 0 to numBytesToRead.
            int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);

            // Break when the end of the file is reached.
            if (n == 0)
                break;

            // Do here what you want to do with the bytes read (convert to string using Encoding.YourEncoding.GetString())
        }
    }

这篇关于如何读取文本文件中的特定部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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