C++ Builder xe8 安卓開發之使用Memo儲存資料的方法
阿新 • • 發佈:2019-01-23
有時候我們在應用程式輸入了一些資料,想下次開啟程式的時候這些資料不會消失,那麼我們就需要把這些資料做一些儲存,下面是其中一種簡單的方法。使用了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中輸入文字後,單擊儲存,然後退出再進入應用,單機讀取,就會看到之前儲存的資料。