写一些文字并保存在我的安卓手机上

Dej*_*jan 1 delphi android firemonkey

我正在尝试在我的 android 手机上的文件 (test.txt) 上保存一些文本,但没有成功。这是我的代码示例:

procedure TDM.WriteLog(text:string);
var
   myFile : TextFile;


 begin


   // Try to open the Test.txt file for writing to
   AssignFile(myFile, System.IOUtils.TPath.Combine(System.IOUtils.tpath.getdocumentspath,'test.txt'));
   ReWrite(myFile);

   // Write a couple of well known words to this file
   WriteLn(myFile, text + sLineBreak );


   // Close the file
   CloseFile(myFile);


 end;
Run Code Online (Sandbox Code Playgroud)

当我在手机上找到文件时,我打开文件但它是空的。我错过了什么?

Alb*_*ola 5

当你想在 Android 中创建一个文本文件(当然 iOS 和 macOS 是一样的)你可以使用WriteAllText属于 TFile 类的类过程。您必须添加使用子句System.IOUtils

TFile.WriteAllText(TPath.Combine(TPath.GetDocumentsPath, 'test.txt'), 'content');
Run Code Online (Sandbox Code Playgroud)

当您想要存储文本文件或与您的应用程序相关的一般数据时,建议您使用 GetDocumentsPath。这是一种创建文本文件的现代方法,但它不是唯一的方法;还请考虑您可以:

  1. 使用 TStringlist 然后调用 SaveToFile('path')
  2. 如果您使用的是备忘录,它具有以下SaveToFile('path')功能
  3. 使用TStreamWriter类(和 StreamReader 读取内容)