加载和显示大型文本文件 [英] Loading and displaying large text files

查看:109
本文介绍了加载和显示大型文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swing应用程序中,我有时需要支持对加载缓慢的大型面向行的文本文件的只读访问:日志,转储,跟踪等。对于少量数据,适用 文档 JTextComponent 没问题,如此处所示。我理解浏览大量数据的人为限制,但有问题的东西似乎总是在最大的文件中。对于10-100兆字节,百万行范围内的大量文本,是否有任何实用的替代方案?

In a Swing application, I sometimes need to support read-only access to large, line-oriented text files that are slow to load: logs, dumps, traces, etc. For small amounts of data, a suitable Document and JTextComponent are fine, as shown here. I understand the human limitations of browsing large amounts of data, but the problematic stuff seems like it's always in the biggest file. Is there any practical alternative for larger amounts of text in the 10-100 megabyte, million-line range?

推荐答案

我会分开问题。

第一个是模型 - 文档构建速度

The first one is model - Document building speed

第二个是文档渲染 - 构建用于表示文档的视图树。

The second is the Document rendering - building tree of views to represent the Document.

问题是您是否需要像关键字一样的字体效果着色?

A question is whether you need font effects like keywords colorizing?

我将从文档构建部分开始。通过EditorKit.read()读取文件的IMHO即使对于大文件也应该快速。我会使用PainDocument来检查纯模型是否构建得足够快,适合您的应用程序。如果是的话,只需使用Document作为模型。如果没有实现你自己的Document接口,因为AbstractDocument有很多方法可以进行更新处理(例如writeLock)。

I would start from Document building part. IMHO reading the file via EditorKit.read() should be fast even for big files. I would use the PainDocument for the purpose and check whether the pure model is built fast enough for your application. If yes it's fine just use the Document as model. If not implement your own Document interface because AbstractDocument has plenty of methods for update processing (e.g. writeLock).

当我们有足够快的文档加载时我们必须解决文档渲染。默认情况下,javax.swing.text中使用的视图非常灵活。它们被设计为要扩展的基类 - 因此有很多我们不需要的代码。例如。测量。

When we have the Document loading fast enough we have to solve the Document rendering. BY default the views used in javax.swing.text are really flexible. They are designed as base classes to be extended - thus has a lot of code we don't need. E.g. measuring.

对于我将使用Monospaced字体的功能,我们不需要换行,因此视图的测量值很快=最长行字符数* char widht。

For the feature I would use Monospaced font, we don't need wrap so measurements of the view widht is fast = longest row char count * char widht.

高度也是字符高度*行数。

The height is also char height * amount of lines.

所以我们的PLainTextViewReplacement非常快。此外,我们不必渲染整个视图,只需在滚动窗格中显示一个片段。因此渲染速度可能会快得多。

So our PLainTextViewReplacement is really fast. Also we don't have to render the whole view but just a fragment visible in our scroll pane. Thus rendering could be mmuch much faster.

当然应该有很多工作来提供正确的插入符号导航,选择等。

Of course there should be a lot of work to provide correct caret navigation, selection etc.

这篇关于加载和显示大型文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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