Delphi Read-Write UTF-8, Unicode format text file not tested

Source: Internet
Author: User

//UTF-8 File Write functionprocedureSaveutffile (ConstFileName:string; Sstring; Writeheader:boolean =True);varMemstream:tmemorystream; headerstr:string;beginifS ="'  ThenExit; Memstream:= Tmemorystream.Create;TryifWriteheader ThenbeginHeaderstr:=# $EF # $BB # $BF; Memstream.write (headerstr[1],3);End; S: =AnsiToUtf8 (S); Memstream.write (s[1], Length (S)); Memstream.position:=0; Memstream.savetofile (FileName);finallyMemstream.free;End;End;//UtF-8 file read functionfunctionLoadutffile (ConstFileName:string; Readheader:boolean = True):string;varMemstream:tmemorystream; S, Headerstr:string;beginResult:="';if  notFileExists (FileName) ThenExit; Memstream:= Tmemorystream.Create;TryMemstream.loadfromfile (FileName);ifReadheader ThenbeginSetLength (HEADERSTR,3); Memstream.read (headerstr[1],3);ifHeaderstr = # $EF # $BB # $BF ThenbeginSetLength (S, Memstream.size-3); Memstream.read (s[1], Memstream.size-3);End;End ElsebeginSetLength (S, memstream.size); Memstream.read (s[1], memstream.size);End; Result:=Utf8toansi (S);finallyMemstream.free;End;End;//Unicode file Write functionprocedureSaveunicodefile (ConstFileName:string;ConstSstring; Writeheader:boolean =True);varMemstream:tmemorystream; HEADERSTR:string; wstr:widestring;beginifS ="'  Thenexit; Memstream:= Tmemorystream.Create;TryifWriteheader ThenbeginHeaderstr:=# $FF # $FE; Memstream.write (headerstr[1],2);End; WSTR:=widestring (S); Memstream.write (wstr[1], Length (WSTR) *2); Memstream.position:=0; Memstream.savetofile (FileName);finallyMemstream.free;End;End;//Unicode file read functionfunctionLoadunicodefile (ConstFileName:string; Readheader:boolean = True):string;varMemstream:tmemorystream; flagstr:string; wstr:widestring;beginResult:="';if  notFileExists (FileName) ThenExit; Memstream: = Tmemorystream.Create;TryMemstream.loadfromfile (FileName);ifReadheader ThenbeginSetLength (FLAGSTR,2); Memstream.read (flagstr[1],2);ifFlagstr = # $FF # $FE ThenbeginSetLength (Wstr, (Memstream.size-2)Div 2); Memstream.read (wstr[1], Memstream.size-2);End;End ElsebeginSetLength (wstr, Memstream.sizeDiv 2); Memstream.read (wstr[1], memstream.size);End; Result:=ansistring (WSTR);finallyMemstream.free;End;End;//Save FileprocedureSaveToFile (ConstFileName:string;ConstSstring);varF:textfile;beginAssignFile (F, FileName);//Make a connection to the file with the F variable, and you can use the F variable to manipulate the file later. Rewrite (F);//to open a file in an additional wayWriteln (F, S);//appends the contents of the S variable to the end of the text. CloseFile (F);//Close FileEnd;
typeTtextformat=(Tfansi,tfunicode,tfunicodebigendian,tfutf8);ConstTextformatflag:Array[Tfansi. TfUtf8] ofWord= ($0000, $FFFE, $FEFF, $EFBB); functionWordlohiexchange (W:word): Word;Register;ASMXCHG AL, AHEnd; {textformat Return text encoding type, stext unprocessed text}procedureReadTextFile (ConstFileName:string; varTextformat:ttextformat;varStext:string);varW:word; B:byte;begin   withTFileStream.Create(Filename,fmopenreadorFmsharedenynone) Do  TryRead (W,2); W:=wordlohiexchange (w);//because it is read in the Word data type, the high and low byte interchange    ifW = Textformatflag[tfunicode] ThenTextFormat:=TfunicodeElse ifW = Textformatflag[tfunicodebigendian] ThenTextFormat:=TfunicodebigendianElse ifW = Textformatflag[tfutf8] Then    beginRead (b,1);//note here that UFT-8 must skip three bytes. textformat:=TfUtf8; End Else    beginTextFormat:=Tfansi; Position:=0; End; SetLength (Stext,size-Position); Readbuffer (stext[1],size-Position); finallyFree ; End;End;

Delphi Read-Write UTF-8, Unicode format text file not tested

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.