How to read very large text files fast
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的更多相关文章
- 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: ...
- 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 ...
- LaF: Fast Access to Large ASCII Files
貌似可以随机读取dataframe格式的文本文件.
- tomcat gzip compression not working for large js files
solution 1: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout=&quo ...
- 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 ...
- text files and binary files
https://en.wikipedia.org/wiki/Text_file https://zh.wikipedia.org/wiki/文本文件
- 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_ ...
- 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 ...
- GIT之二 基础篇(1)
GIT基础 取得项目的 Git 仓库 有两种取得 Git 项目仓库的方法.第一种是在现存的目录下,通过导入所有文件来创建新的 Git 仓库.第二种是从已有的 Git 仓库克隆出一个新的镜像仓库来. 在 ...
随机推荐
- bzoj 3572 [Hnoi2014]世界树——虚树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3572 关于虚树:https://www.cnblogs.com/zzqsblog/p/556 ...
- POJ2559最大矩形面积——单调栈
题目:http://poj.org/problem?id=2559 #include<iostream> #include<cstdio> using namespace st ...
- REST风格框架:从MVC到前后端分离***
摘要: 本人在前辈<从MVC到前后端分离(REST-个人也认为是目前比较流行和比较好的方式)>一文的基础上,实现了一个基于Spring的符合REST风格的完整Demo,具有MVC分层结构并 ...
- serialize unserialize
转自 http://www.cnblogs.com/yeer/archive/2009/03/25/1421161.html php函数serialize()与unserialize() seri ...
- 【Oracle学习笔记-1】Win7下安装Oracle 10g
源程序获取 从Oracle的官网上下载Oracle 10g: 10203_vista_w2k8_x86_production_db.zip:==>服务器(必须) 10203_vista_w2k8 ...
- bzoj1050 旅行
Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大 ...
- HDU 1116 Play on Words(并查集和欧拉回路)(有向图的欧拉回路)
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- COMMON INTERVIEW QUESTIONS
1. What do you see yourself doing five years from now? 2. What motivates you to put forth your great ...
- Mac parallels desktop安装windows,linux
前言 这款软件你就看作是虚拟机vm,如果你要安装win10系统,请下载ios镜像文件 下载准备工作 Parallels Desktop 13 破解版本 联系站长所要 win10 iso镜像文件 ...
- C#后台调用前台javascript的五种方法小结
第一种,OnClientClick (vs2003不支持这个方法) <asp:Button ID="Button1" runat="server" Tex ...