Loading UTF-8 files

<< Click to display table of contents >>

Loading UTF-8 files

These examples show how to load Unicode UTF-8 file.

Example 1

procedure LoadUTF8(rv: TCustomRichViewconst FileName: String

  StyleNo, ParaNo: Integer); 

var Stream: TFileStream; 

    s: TRVRawByteString

    ws: TRVUnicodeString

begin 

  // loading UTF-8 in s

  Stream := TFileStream.Create(FileName, fmOpenRead); 

  SetLength(s, Stream.Size); 

  Stream.ReadBuffer(PRVAnsiChar(s)^, Stream.Size); 

  Stream.Free; 

  rv.Clear; 

  // converting from s (UTF-8) to ws (UTF-16)

  ws := UTF8Decode(s); 

  // adding ws in document

  rv.AddTextNLW(ws, StyleNo, ParaNo, ParaNo, False); 

end

Call:

LoadUTF8(RichViewEdit1, 'test.txt'00); 

RichViewEdit1.Format

Example 2

RichViewEdit1.Clear; 

RichViewEdit1.LoadText('test.txt', 0, 0, False, CP_UTF8);

RichViewEdit1.Format; 

See also:

Unicode in TRichView.