textbox

IT博客 联系 聚合 管理
  103 Posts :: 7 Stories :: 22 Comments :: 0 Trackbacks
  procedure SaveFile( const  FileName:  string ; Source:  string ;
    encoding: TEncoding);
  var
    sl: TStrings;
    memoStream: TFileStream;
    buff: Tbytes;
    LByteOrderMark: TBytes;
  begin
    
if  Source  =   ' ' then
       exit ;
    try
      buff :
=  encoding.GetBytes(Source);
      memoStream :
=  TFileStream.Create(FileName, fmCreate);
      LByteOrderMark :
=  encoding.GetPreamble;
      memoStream.Write(LByteOrderMark[
0 ], Length(LByteOrderMark));
      memoStream.Write(buff[
0 ], length(buff));
    finally
      memoStream.Free;
    
end ;
  
end ;

      SaveFile('C:\Temp\test_' + 'ascii_gbk.txt', ss, TEncoding.GetEncoding(936));//gbk
      SaveFile('C:\Temp\test_' + 'ascii_bg5.txt', ss, TEncoding.GetEncoding(950));//bg5 繁体
      SaveFile('C:\Temp\test_' + 'utf8.txt', ss, TEncoding.UTF8);
      SaveFile('C:\Temp\test_' + 'unicode.txt', ss, TEncoding.Unicode);

unicode转中文的时候必须要制定页码,不然会出现乱码.比如的以下这两句.
  SaveFile('C:\Temp\test_' + 'ascii_gbk.txt', ss, TEncoding.GetEncoding(936));//gbk
  SaveFile('C:\Temp\test_' + 'ascii_bg5.txt', ss, TEncoding.GetEncoding(950));//bg5 繁体
posted on 2011-03-03 10:28 零度 阅读(2653) 评论(0)  编辑 收藏 引用 所属分类: delphi
只有注册用户登录后才能发表评论。