有时候我们在应用程序输入了一些数据,想下次打开程序的时候这些数据不会消失,那么我们就需要把这些数据做一些保存,下面是其中一种简单的方法。使用了Memo控件的SaveToFile和LoadFromFile方法。
主要源码如下:
//---------------------------------------------------------------------------
#include <fmx.h>
#pragma hdrstop
#include "Unit2.h"
#include <System.IOUtils.hpp> //文件路径接口,必须包含
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
Memo1->Lines->SaveToFile(System::Ioutils::TPath::GetTempPath() + "/test.txt"); //保存文件
ShowMessage(L"已经保存到 " + System::Ioutils::TPath::GetTempPath() + L"/test.txt 文件夹下"); //显示对话框
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button2Click(TObject *Sender)
{
Memo1->Lines->LoadFromFile(System::Ioutils::TPath::GetTempPath() + "/test.txt"); //读取文件
ShowMessage(L"从" + System::Ioutils::TPath::GetTempPath() + L"/test.txt 读取数据"); //显示对话框
}
//---------------------------------------------------------------------------
在Memo中输入文字后,单击保存,然后退出再进入应用,单机读取,就会看到之前保存的数据。