同样这段代码读unicode文本文件就可以,大家帮看看
var
pBuffer: PByte;
ff,pwChr: pWideChar;
strmScript: TMemoryStream;
sScript: string;
i:integer;
begin
strmScript:=TMemoryStream.Create;
TBlobField(adoQuery1.FieldByName('blob')).SaveToStream(strmScript);
GetMem(pBuffer, strmScript.Size);
try
strmScript.Read(pBuffer^, strmScript.Size);
pwChr:=pWideChar(pBuffer
//Inc(pwChr,2);
sScript :=WideCharToString(pwChr);
SetLength(sScript,strmScript.Size div 2);//必须限制大小否则有乱码
ShowMessage(IntToStr(strmScript.Size));
Memo1.Text :=sScript;
RichEdit1.Lines.Text:=sScript;
finally
FreeMem(pBuffer);
strmScript.Free;
end;
var
pBuffer: PByte;
ff,pwChr: pWideChar;
strmScript: TMemoryStream;
sScript: string;
i:integer;
begin
strmScript:=TMemoryStream.Create;
TBlobField(adoQuery1.FieldByName('blob')).SaveToStream(strmScript);
GetMem(pBuffer, strmScript.Size);
try
strmScript.Read(pBuffer^, strmScript.Size);
pwChr:=pWideChar(pBuffer
//Inc(pwChr,2);
sScript :=WideCharToString(pwChr);
SetLength(sScript,strmScript.Size div 2);//必须限制大小否则有乱码
ShowMessage(IntToStr(strmScript.Size));
Memo1.Text :=sScript;
RichEdit1.Lines.Text:=sScript;
finally
FreeMem(pBuffer);
strmScript.Free;
end;
Memo1.Text :=sScript;VCL不支持unicode字符集.方法如下:
(一)你可以使用TntUnicodeControls组件。 下载地址:http://download.tntware.com/delphi_unicode_controls/TntUnicodeControls.zip (二)自己编写读取的过程进行转换实现unicode文件的读取,将unicode->Ansi function GetFileText(const FileName: string): string;
var
hFile: DWORD;
iSize: DWORD;
sTemp: string;
begin
// 打开文件
hFile := CreateFile(PChar(FileName), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
// 文件大小
iSize := GetFileSize(hFile, nil);
// 申请内存
SetLength(sTemp, iSize + 2);
// 读入文件
ReadFile(hFile, sTemp[1], iSize, iSize, nil);
// 关闭文件
CloseHandle(hFile);
// 置结束符
sTemp[iSize + 1] := #0;
sTemp[iSize + 2] := #0;
// 编码格式
if (sTemp[1] = #$FF) and (sTemp[2] = #$FE) then // UNICODE
begin
// 申请内存
SetLength(Result, iSize);
// 转换编码
WideCharToMultiByte(CP_ACP, 0, PWideChar(@sTemp[3]), -1, @Result[1], iSize, nil, nil);
end else
Result := sTemp;
// 去掉多余字符
Result := string(PChar(Result));
end;
TMemoryStream.LoadFromFile(c:\文本)处理后就可以显示正确而在TBlobField(adoQuery1.FieldByName('blob')).SaveToStream(strmScript);在变量中传递再处理就是乱码呢不可能每次都把文件读出来存上,再用TMemoryStream读出处理吧!!!问题可能不太难,我刚学Delphi,高手指点一下