Question
Does anyone know the fastest way to read large text files (10Mb) into a string.Readln is just too slow.
 

Answer 1

You may try this:

function R(const FileName: string): string;
var
M: TFileStream;
begin
M := TFileStream.Create(FileName, fmOpenRead);
try
SetLength(Result, M.Size);
M.Read(Result[1], M.Size);
finally
M.Free;
end;
end;

Answer 2

As an alternative to Christian's suggestion, you can also use a memory-mapped file:

function MMFileToString(const AFilename: string): string;
var
hFile: THandle;
hFileMap: THandle;
hiSize: DWORD;
loSize: DWORD;
text: string;
view: pointer;
begin
Result := '';
if AFilename = '' then
Exit;
if not FileExists(AFilename) then
Exit;
{Open the file}
hFile := CreateFile(
PChar(AFilename), GENERIC_READ, FILE_SHARE_READ, nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
);
if hFile <> INVALID_HANDLE_VALUE then
begin
loSize := GetFileSize(hFile, @hiSize);
{File was opened successfully, now map it:}
hFileMap := CreateFileMapping(
hFile, nil, PAGE_READONLY, hiSize, loSize, 'TextForString'
);
if (hFileMap <> 0) then
begin
if (GetLastError() = ERROR_ALREADY_EXISTS) then
begin
MessageDlg(
'Mapping already exists - not created.', mtWarning, [mbOk], 0
);
CloseHandle(hFileMap)
end
else
begin
try
{File mapped successfully, now map a view of the file into the
address space:}
view := MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
if (view <> nil) then
begin {View mapped successfully}
{Close file handle - as long is view is open it will persist}
CloseHandle(hFile);
SetLength(Result, loSize);
Move(view^, Result[1], loSize);
end
else
MessageDlg(
'Unable to map view of file. ' + SysErrorMessage(GetLastError),
mtWarning, [mbOk], 0
);
finally
UnmapViewOfFile(view); {Close view}
CloseHandle(hFileMap); {Close mapping}
end
end
end
else
begin
MessageDlg(
'Unable to create file mapping. ' + SysErrorMessage(GetLastError),
mtWarning, [mbOk], 0
);
end;
end
else
begin
MessageDlg(
'Unable to open file. ' + SysErrorMessage(GetLastError),
mtWarning, [mbOk], 0
);
end;
end;

How to read very large text files fast的更多相关文章

  1. Writing Text Files On The Client in Oracle Forms 10g

    Below is the example to write file on client in Oracle Forms 10g with webutil library package.Note:  ...

  2. Access text files using SQL statements by DB Query Analyzer

    Access text files using SQL statements by DB Query Analyzer Ma Gen feng (Guangdong Unitoll Services ...

  3. LaF: Fast Access to Large ASCII Files

    貌似可以随机读取dataframe格式的文本文件.

  4. tomcat gzip compression not working for large js files

    solution 1: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout=&quo ...

  5. How to remove duplicate lines in a large text file?

    How would you remove duplicate lines from a file that is  much too large to fit in memory? The dupli ...

  6. text files and binary files

    https://en.wikipedia.org/wiki/Text_file https://zh.wikipedia.org/wiki/文本文件

  7. interleave two text files with specified lines

    a_file=$1 a_step=$2 b_file=$3 b_step=$4 a_start=1 let a_end=$a_start+$a_step b_start=1 let b_end=$b_ ...

  8. Convert between Unix and Windows text files - IU Knowledge Base from: https://kb.iu.edu/d/acux

    vi. To input the ^M character, press Ctrl-v , and then press Enter or return . In vim, use :set ff=u ...

  9. GIT之二 基础篇(1)

    GIT基础 取得项目的 Git 仓库 有两种取得 Git 项目仓库的方法.第一种是在现存的目录下,通过导入所有文件来创建新的 Git 仓库.第二种是从已有的 Git 仓库克隆出一个新的镜像仓库来. 在 ...

随机推荐

  1. HttpFileCollection类

    最近在学HttpRequest类搞文件上传的时候看到Request.Files返回了HttpFileCollection 这个类的一个对象,这个类用于获取浏览器上传的文件集合,在文件上传的时候可以通过 ...

  2. MySql数据库中敏感字段加密处理方案

    比如数据表中有一个手机号码字段是敏感字段,不想明文存储,可以是用MySQL的自带的函数处理 Table 12.17 Encryption Functions Name Description AES_ ...

  3. JS时间(日期)比较或相减(暂时停用)

    注:此文均来自网上,可行,只供参考 //JAVASCRIPT中 日期相减很麻烦 ,现在有现成的实现方法,拷贝过去就可以用了,方便 //调用该方法(主方法) function dateDiff(date ...

  4. Mysql 性能优化1 硬件设备的选择

    --------------------------------------------目录------------------------------------------------- • 我们 ...

  5. 发邮件 文字+ 附件的方法(QQ or 网易 邮箱)

    #coding:utf-8import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIME ...

  6. csrf xss sql注入

    1.输入框 sql注入 测试直接在输入框输入1' ,看sql会不会拼接出错 xss攻击 csrf攻击 测试直接在输入框输入 <script>alert(123)</script> ...

  7. python-并发测试用例

    以前看了虫师的并发,然后觉得以后如果遇上领导要求一个模块里的并发怎么办,然后就想到了下面的方法: 代码: 在原有的基础下再往casedir数组加模块三里面细分的对象.(这里可以封装成函数调用,工作需要 ...

  8. java 调用windows bat脚本

    当我们需要在java程序中调用外部程序,我们可用通过Runtime.exec()调用来完成. The class java.lang.Runtime features a static method ...

  9. tomcat启动窗口报错&&eclipse使用maven编译时报错

    tomcat启动窗口报错log4j:ERROR Could not find value for key log4j.appender.stdoutlog4j:ERROR Could not inst ...

  10. 1006 Sign In and Sign Out (25 分)

    1006 Sign In and Sign Out (25 分) At the beginning of every day, the first person who signs in the co ...