Csharp/C#教程:如何阅读文本文件中的特定部分?分享


如何阅读文本文件中的特定部分?

我有一个非常大的文本文件(500mb),我需要得到它的文本。 当然问题是内存exception,但我想用字符串(或char数组)解决它并将它们放在List中。 我在谷歌搜索,我真的不知道如何采取特定的部分。 *如果有帮助的话,这是一条很长的路线。

去做:

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()) } } 

您可以使用StreamReader类来读取文件的某些部分。

上述就是C#学习教程:如何阅读文本文件中的特定部分?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/996785.html

(0)
上一篇 2021年12月25日
下一篇 2021年12月25日

精彩推荐