Delphi How to save and read utf-8 text files

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.