如何有效地阅读Delphi中的许多文件的第一行 [英] How Can I Efficiently Read The FIrst Few Lines of Many Files in Delphi

查看:194
本文介绍了如何有效地阅读Delphi中的许多文件的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一个查找文件功能,可以找到我的程序读取的.ged后缀的文本文件。我在一个像Explorer这样的浏览器窗口中显示找到的结果:

I have a "Find Files" function in my program that will find text files with the .ged suffix that my program reads. I display the found results in an explorer-like window that looks like this:

我使用标准的FindFirst / FindNext方法,这个工作非常快。上面显示的584个文件可以在几秒钟内找到并显示。

I use the standard FindFirst / FindNext methods, and this works very quickly. The 584 files shown above are found and displayed within a couple of seconds.

我现在想做的是向显示屏中添加两列,显示Source 和版本,其中包含在这些文件中。这些信息通常位于每个文件的前10行中,行如下:

What I'd now like to do is add two columns to the display that shows the "Source" and "Version" that are contained in each of these files. This information is found usually within the first 10 lines of each file, on lines that look like:

1 SOUR FTM
2 VERS Family Tree Maker (20.0.0.368)

现在我很快自己解析很快,这不是我所要求的。

Now I have no problem parsing this very quickly myself, and that is not what I'm asking.

我需要帮助的是简单的如何最快地加载这些文件中的前10行,以便我可以解析它们。

What I need help with is simply how to most quickly load the first 10 or so lines from these files so that I can parse them.

我试图做一个StringList.LoadFromFile,但是加载大文件需要太多时间,比如1 MB以上的大文件。

I have tried to do a StringList.LoadFromFile, but it takes too much time loading the large files, such at those above 1 MB.

由于我只需要前10行,我最好如何获得他们?

Since I only need the first 10 lines or so, how would I best get them?

使用Delphi 2009,我的输入文件可能是Unicode,也可能不是Unicode,所以这需要适用于任何编码。

I'm using Delphi 2009, and my input files might or might not be Unicode, so this needs to work for any encoding.

跟随:感谢安东尼奥,

我最终做了这个工作正常:

I ended up doing this which works fine:

var
  CurFileStream: TStream;
  Buffer: TBytes;
  Value: string;
  Encoding: TEncoding;

try
  CurFileStream := TFileStream.Create(folder + FileName, fmOpenRead);
  SetLength(Buffer, 256);
  CurFileStream.Read(Buffer[0], 256);
  TEncoding.GetBufferEncoding(Buffer, Encoding);
  Value := Encoding.GetString(Buffer);
  ...
  (parse through Value to get what I want)
  ...
finally
  CurFileStream.Free;
end;


推荐答案

使用TFileStream和读取方法读取需要的字节数。以下是读取还存储在文件开头的位图信息的示例。

Use TFileStream and with Read method read number of bytes needed. Here is the example of reading bitmap info that is also stored on begining of the file.

http://www.delphidabbler.com/tips/19

这篇关于如何有效地阅读Delphi中的许多文件的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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