为大文本文件实现阅读器的最佳策略 [英] Best strategy to implement reader for large text files

查看:26
本文介绍了为大文本文件实现阅读器的最佳策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个应用程序将其处理步骤记录到文本文件中.这些文件在实施和测试期间用于分析问题.每个文件最大为 10MB,最多包含 100,000 行文本.

We have an application which logs its processing steps into text files. These files are used during implementation and testing to analyse problems. Each file is up to 10MB in size and contains up to 100,000 text lines.

目前对这些日志的分析是通过打开文本查看器(Notepad++ 等)并根据问题查找特定字符串和数据来完成的.

Currently the analysis of these logs is done by opening a text viewer (Notepad++ etc) and looking for specific strings and data depending on the problem.

我正在构建一个有助于分析的应用程序.它将使用户能够读取文件、搜索、突出显示特定字符​​串以及与隔离相关文本相关的其他特定操作.

I am building an application which will help the analysis. It will enable a user to read files, search, highlight specific strings and other specific operations related to isolating relevant text.

文件不会被编辑!

在玩一些概念时,我立即发现 TextBox(或 RichTextBox)不能很好地处理大文本的显示.我设法使用 DataGridView 以可接受的性能实现了一个查看器,但该控件不支持特定字符串的颜色突出显示.

While playing a little with some concepts, I found out immediately that TextBox (or RichTextBox) don't handle display of large text very well. I managed to to implement a viewer using DataGridView with acceptable performance, but that control does not support color highlighting of specific strings.

我现在考虑将整个文本文件作为字符串保存在内存中,并且只在 RichTextBox 中显示数量非常有限的记录.对于滚动和导航,我想添加一个独立的滚动条.

I am now thinking of holding the entire text file in memory as a string, and only displaying a very limited number of records in the RichTextBox. For scrolling and navigating I thought of adding an independent scrollbar.

我对这种方法的一个问题是如何从存储的字符串中获取特定的行.

One problem I have with this approach is how to get specific lines from the stored string.

如果有人有任何想法,可以指出我的方法存在的问题,谢谢.

If anyone has any ideas, can highlight problems with my approach then thank you.

推荐答案

我建议将整个内容加载到内存中,但作为字符串的集合而不是单个字符串.这样做很容易:

I would suggest loading the whole thing into memory, but as a collection of strings rather than a single string. It's very easy to do that:

string[] lines = File.ReadAllLines("file.txt");

然后你可以用 LINQ 搜索匹配的行,轻松显示它们等

Then you can search for matching lines with LINQ, display them easily etc.

这篇关于为大文本文件实现阅读器的最佳策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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