简体   繁体   English

在Delphi Android中使用TextFile

[英]using TextFile in Delphi Android

In my Delphi 10 Seattle app, I am reading a text file (actually an ini file) using TextFile as below: 在我的Delphi 10 Seattle应用程序中,我正在使用TextFile读取文本文件(实际上是ini文件),如下所示:

    SDPath := ClientUtils.GetSharedPath;
    FileName := TPath.Combine(SDPath, 'Client.ini');
    if not MemoVisible then
    begin
      Panel2.Visible := True;
      if FileExists(FileName) then
      begin
        MemoConfig.Lines.Clear;
        AssignFile(T, FileName);
        try
          Reset(T);
          while not Eof(T) do
          begin
            ReadLn(T, text);
            MemoConfig.Lines.Add(text)
          end;
        finally
          CloseFile(T);
        end;
      end

I noticed that the first line contains [Configuration] , 3 additional characters that weren't in the original file. 我注意到第一行包含[Configuration] ,这3个其他字符不在原始文件中。 I'm guessing this is from when the client.ini file was deployed from my Windows 7 PC. 我猜这是从Windows 7 PC上部署client.ini文件时得出的。 After I have edited the characters out and saved the file with 在我编辑完字符并用

      AssignFile(T, FileName);
      try
        Rewrite(T);
        for i := 0 to MemoConfig.Lines.Count -1 do
          WriteLn(T, MemoConfig.Lines[i]);
      finally
         CloseFile(T)
      end;

the file remains correct. 该文件仍然正确。 Where did the extra characters comes from and what can I do to prevent them from getting into the file? 多余的字符是从哪里来的,我该怎么做以防止它们进入文件?

Those characters are a byte-order mark indicating that your file is encoded as UTF-8. 这些字符是字节顺序的标记,指示您的文件被编码为UTF-8。 The classic Delphi file-reading functions aren't aware of byte-order marks. 经典的Delphi文件读取功能不知道字节顺序标记。

To prevent them from appearing in your file, consult the documentation for the program you used to create the file. 为防止它们出现在文件中,请查阅用于创建文件的程序的文档。 You might have the option of excluding the mark explicitly, or you might be able to select a different encoding. 您可以选择显式排除该标记,也可以选择其他编码。

You might wish to avoid further use of the classic text-file functions. 您可能希望避免进一步使用经典的文本文件功能。 You could replace most of your code with a single function call: 您可以用一个函数调用来替换大多数代码:

MemoConfig.Lines.LoadFromFile(FileName);

That function also offers additional parameters to let you be explicit about what text encoding to use when reading the file. 该函数还提供了其他参数,使您可以清楚地了解在读取文件时使用哪种文本编码。 You should always know what text encoding you're using, especially with files that you're in control of like this one. 您应该始终知道您使用的是哪种文本编码,尤其是对于您这样控制的文件。

Those characters are the Unicode UTF-8 byte order markers (EFBBBF hex). 这些字符是Unicode UTF-8字节顺序标记(十六进制EFBBBF)。

You can prevent them from coming back by properly saving the file from whatever text editor you're using to create it. 您可以通过使用创建文件时使用的任何文本编辑器正确保存文件来防止文件再次出现。 Save the file as ASCII or ANSI format text. 将文件另存为ASCII或ANSI格式的文本。

As far as your code, it's doing a lot more work than it should. 就您的代码而言,它所做的工作比应做的更多。 You can load the file into your memo much more easily: 您可以更轻松地将文件加载到备忘录中:

SDPath := ClientUtils.GetSharedPath;
FileName := TPath.Combine(SDPath, 'Client.ini');
if not MemoVisible then
begin
  Panel2.Visible := True;
  if FileExists(FileName) then
  begin
    MemoConfig.Lines.Clear;
    MemoConfig.Lines.LoadFormFile(FileName);
  end;
end;

Your code to save the file then becomes the converse: 您保存文​​件的代码将变得相反:

MemoConfig.Lines.SaveToFile(FileName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM