Delphi How to save and read utf-8 text files
Original from: 100 Script web
Delphi Save Read Utf-8 text file
To the customer to do a batch image recognition and synthesis of double-layer PDF program, the final customer needs to generate a copy of the Notepad file, is the text after the OCR, and specify the UTF-8 format. There was a little bit of a problem when dealing with utf-8, and now it's summarized as follows
First, the Utf8encode function of Delphi is used to convert ordinary characters to utf-8 encoding.
Create a stream that MemoryStream or FileStream can
The function looks like this
Reference
Procedure Saveutf8file (acontent:widestring; afilename:string);
Var
Ffilestream:tfilestream;
futf8bytes:string;
s:string;
Begin
Ffilestream:=tfilestream.create (afilename,fmcreate);
futf8bytes:= Utf8encode (acontent);
Ffilestream.write (Futf8bytes[1],length (futf8bytes));
Ffilestream.free;
End
After running the view generated files, all garbled, Internet search found
Unicode text file: The first two characters are FF FE (16 binary)
Utf-8 text file: The first two characters are EF BB (16 binary)
I forgot to put the file in the head.
So after adding the code
Reference
Procedure Saveutf8file (acontent:widestring; afilename:string);
Var
Ffilestream:tfilestream;
futf8bytes:string;
s:string;
Begin
Ffilestream:=tfilestream.create (afilename,fmcreate);
futf8bytes:= Utf8encode (acontent);
s:=# $EF # $BB # $BF;
Ffilestream.write (S[1],length (S));
Ffilestream.write (Futf8bytes[1],length (futf8bytes));
Ffilestream.free;
End
After saving the file to view, or garbled. Find a half-day problem finally found that the problem appears in the declaration of the parameter widestring, changed to a string is no problem.
The last generated code is as follows
Reference
Procedure Saveutf8file (acontent:string; afilename:string);
Var
Ffilestream:tfilestream;
futf8bytes:string;
s:string;
Begin
Ffilestream:=tfilestream.create (afilename,fmcreate);
futf8bytes:= Utf8encode (acontent);
s:=# $EF # $BB # $BF;
Ffilestream.write (S[1],length (S));
Ffilestream.write (Futf8bytes[1],length (futf8bytes));
Ffilestream.free;
End
Attach a code that reads Utf-8 text
Reference
function Loadutf8file (afilename:string): string;
Var
Ffilestream:tfilestream;
fansibytes:string;
s:string;
Begin
Ffilestream:=tfilestream.create (Afilename,fmopenread);
SetLength (s,ffilestream.size);
Ffilestream.read (S[1],length (S));
fansibytes:= Utf8decode (Copy (s,4,maxint));
Result:= fansibytes;
End
Delphi How to save and read utf-8 text files
Delphi How to save and read utf-8 text files