相关疑难解决方法(0)

StreamReader的性能很差,同时读取非常大的文件

我需要在C#应用程序上同时逐行读取四个非常大(> 2 Gb)的文件.我正在使用四种不同的StreamReader对象及其ReadLine()方法.同时从四个文件读取线条时性能受到严重影响,但是每个文件到达EoF时都会变得更好(有4个文件的性能<带3个文件的性能<带有2个文件的性能......).

我有这个(简化,假设只有两个文件,更简洁的例子)代码:

StreamReader readerOne = new StreamReader(@"C:\temp\file1.txt");
StreamReader readerTwo = new StreamReader(@"C:\temp\file2.txt");

while(readerOne.Peek() >= 0 || readerTwo.Peek() >= 0)
{
    string[] readerOneFields = readerOne.Peek() >= 0 ? 
        readerOne.ReadLine().Split(',') : null;
    string[] readerTwoFields = readerTwo.Peek() >= 0 ? 
        readerTwo.ReadLine().Split(',') : null;

    if (readerOneFields != null && readerTwoFields != null)
    {
        if (readerOneFields[2] == readerTwoFields[2])
        {
            // Do some boring things...
        }
    else if (readerOneFields != null)
    {
        // ...
    }
    else …
Run Code Online (Sandbox Code Playgroud)

c# io performance streamreader

2
推荐指数
1
解决办法
679
查看次数

逐行读取大型文本文件并搜索字符串

我目前正在开发一个读取大约50000行文本文件的应用程序。对于每一行,我需要检查它是否包含特定的字符串。

此刻,我使用常规System.IO.StreamReader方式逐行读取我的文件。

问题在于文本文件的大小每次都会更改。我进行了几次测试,结果发现当文件大小增加时,读取一行会花费更多的时间。

例如 :

读取包含5000行
的txt文件:0:40 读取包含10000行的txt文件:2:54

读取文件的时间比读取文件大2倍,需要4倍的时间。我无法想象读取100000行文件将花费多少时间。

这是我的代码:

using (StreamReader streamReader = new StreamReader(this.MyPath))
{
     while (streamReader.Peek() > 0)
     {
          string line = streamReader.ReadLine();

          if (line.Contains(Resources.Constants.SpecificString)
          {
               // Do some action with the string.
          }
     }
}
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以避免这种情况:更大的文件=更多的时间来读取一行?

c# string file

1
推荐指数
1
解决办法
3768
查看次数

如何编辑文本文件而不立即读取整个文件

我正试图解决这个问题:

编写一个程序,用文本文件中的"finish"替换每个子串"start".你能改写程序只替换整个单词吗?该程序是否适用于大文件(例如800 MB)?

我一直试图这样做,但显然你不能同时读写.如果有人可以查看我的代码并帮助我,那就太棒了.这是一个例外:

The process cannot access the file 'C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt' because it is being used by another process.

你不必直接给我答案,而是告诉我这个过程.谢谢!

这是我目前的代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Chapter_15_Question_7
{
class Program
{
    static void Main(string[] args)
    {
        StreamReader reader = new StreamReader(
            @"C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt");

        StreamWriter writer = new StreamWriter(
            @"C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter …
Run Code Online (Sandbox Code Playgroud)

c#

1
推荐指数
1
解决办法
802
查看次数

如何在C#中修改11GB .csv文件?

我有一个1500万行.csv文件.它包括一些只有连字符的行.但是用excel,记事本或记事本++打开这个文件是不可能的.因此我认为要在C#中进行修改(首先读入,然后写出作为未建立连字符行的新修改文件).

我怎样才能以最简单的方式编写代码?

c# csv self-modifying bigdata

0
推荐指数
1
解决办法
124
查看次数

标签 统计

c# ×4

bigdata ×1

csv ×1

file ×1

io ×1

performance ×1

self-modifying ×1

streamreader ×1

string ×1