// 2010后 procedure TForm1.Button3Click(Sender: TObject); var pstr:Pchar; mestr:string; FS:TFilestream; sUString: UTF8String; begin FS:=TFileStream.Create((Extractfilepath(Application.ExeName)+'123.txt'),fmCreate or fmOpenWrite); FS.Seek(0, soEnd); mestr:=Memo1.Text; pstr:=Pchar(mestr);//把字符串转成字符指针 sUString := UTF8String(mestr); FS.write(sUString[1], Length(mestr));//正确!XE必须将将字符串编码为UTF8 FS.Free; end;
// 2010前
procedure TForm1.Button7Click(Sender: TObject); var pstr:Pchar; mestr:string; FS:TFilestream; sUString: UTF8String; begin FS:=TFileStream.Create((Extractfilepath(Application.ExeName)+'123.txt'),fmCreate or fmOpenWrite); FS.Seek(0, soEnd); mestr := Memo1.Text; pstr := Pchar(mestr);//把字符串转成字符指针 FS.Writebuffer(pstr^,Length(pstr));//把字符串写入流中 FS.Free; end;
//文件转字节数组
procedure TForm1.Button1Click(Sender: TObject); var AFileData: TByteDynArray; fsize:Integer; FS:TFilestream; begin FS:=TFileStream.Create('D:\a.zip',fmOpenRead); FS.Position:=0;//切记,操作图片前一定要这句,否则错误 //把内存流转化成TByteDynArray fsize:=FS.Size; SetLength(AFileData, fsize); FS.Read(AFileData[0],fsize); ShowMessage('ok'); FS.Free; end;
|
请发表评论