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

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

问题描述

在 Swing 应用程序中,我有时需要支持对加载缓慢的大型、面向行的文本文件的只读访问:日志、转储、跟踪等.对于少量数据,一个 适合 DocumentJTextComponent很好,如此处所示.我理解浏览大量数据的人为限制,但有问题的东西似乎总是在最大的文件中.对于 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() 读取文件也应该很快.我会为此目的使用 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).

当我们的 Document 加载速度足够快时,我们必须解决 Document 渲染问题.默认情况下,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.

对于我将使用等宽字体的功能,我们不需要换行,因此视图宽度的测量速度很快 = 最长行字符数 * 字符宽度.

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天全站免登陆