我试图在我的 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;
当我在手机上找到文件时,我打开了文件,但它是空的。我错过了什么?
最佳答案
当你想在 Android 中创建一个文本文件时(当然 iOS 和 macOS 也是如此)你可以使用 WriteAllText
属于 TFile 类的类过程。您必须在 uses 子句中添加 System.IOUtils
。
TFile.WriteAllText(TPath.Combine(TPath.GetDocumentsPath, 'test.txt'), 'content');
当您要存储文本文件或与您的应用程序相关的一般数据时,建议您使用 GetDocumentsPath。这是一种创建文本文件的现代方法,但不是唯一的方法;还请考虑您可以:
- 使用 TStringlist,然后调用
SaveToFile('path')
- 如果您使用的是备忘录,它具有
SaveToFile('path')
功能 - 使用
TStreamWriter
类(以及用于读取内容的 StreamReader)
关于android - 写一些文字并保存在我的安卓手机上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45098458/