在firemonkey中打开并读取文件

时间:2019-09-06 14:37:25

标签: delphi firemonkey

此代码有什么问题?我不明白,如果我删除了“尝试”,则我的应用程序不会打开,如果不删除,则总是显示“需要登录” ...

procedure TF_login.FormActivate(Sender: TObject);
 var
      Result: Integer;
      TextFile: TStringList;
      VarArquivo: string;
      text: string;
      dataI, dataF : string;
begin
  TextFile := TStringList.Create;
  VarArquivo := System.IOUtils.TPath.GetDocumentsPath + PathDelim + 'Limit.txt';
  try
    TextFile.LoadFromFile(VarArquivo);

    text := TextFile.Text;
//    ShowMessage(TextFile.Text); // there is the text
//    ShowMessage(text); // there is the text

    dataI := FormatDateTime('dd/mm/yyyy', Now);
    dataF := FormatDateTime('dd/mm/yyyy', StrToDate(text));

    Result := CompareDate(StrToDate(dataI), StrToDate(dataF));
    ShowMessage(dataF +' data f');
    ShowMessage(dataI +' data I');
    if ( Result = LessThanValue ) then
    begin
      ShowMessage('data F low');
    end
    else
    begin
      ShowMessage('data F high');
      F_inicio.Show;
    end;

    FreeAndNil(TextFile);
  except on E:
    Exception do ShowMessage('An error happened!' + sLineBreak + '[' + 
                               E.ClassName + '] ' + E.Message);
  end;
end;
  

错误:[EConvertError] '09 / 11/2019'不是有效日期

创建文件,我这样做:

procedure TF_login.btn_entrarClick(Sender: TObject);
var
  data : tdatetime;
  Resposta, data_s: string;
begin
   PathFile := System.IOUtils.TPath.GetDocumentsPath;
   NameFile := 'Limit.txt';
   data := Now; //data actual
   data := IncMonth(data, 2);
   data_s := FormatDateTime('dd/mm/yyyy', data);
   TFile.WriteAllText(TPath.Combine(PathFile, NameFile), data_s );
   F_inicio.Show;
end;

该文件存在,因为第一个(和第二个)ShowMessage(带有注释)向我显示了“ 09/11/19”,但第三个和第四个对我来说却不显示...

OBS:Delphi 10.3(RIO),平台:Android

1 个答案:

答案 0 :(得分:-1)

您应该在代码中更改几件事:

procedure TF_login.FormActivate(Sender: TObject);
 var
      TextFile: TStringList;
      VarArquivo: string;
      text: string;
      dataI, dataF : string;
begin
  // If an exception (unlikely, but on principle) happens in your VarArquivo
  // assignment, then the original version will leak the allocated TStringList.
  // Always place the TRY right after allocation of a memory block. That way
  // you ensure that the FINALLY block will always release the allocated
  // memory. Also, always include a FINALLY block to release the memory. Don't
  // count on your code to reach the FreeAndNIL code (it doesn't in this
  // instance, as you can see) to make sure that you actually release the
  // memory.
  VarArquivo := System.IOUtils.TPath.GetDocumentsPath + PathDelim + 'Limit.txt';
  TextFile := TStringList.Create;
  try // - except
    try // - finally
      TextFile.LoadFromFile(VarArquivo);

      text := TextFile.Text;
  //    ShowMessage(TextFile.Text); // there is the text
  //    ShowMessage(text); // there is the text

      dataI := FormatDateTime('yyyy/mm/dd', Date);
      dataF := FormatDateTime('yyyy/mm/dd', StrToDate(text));
      ShowMessage(dataF +' data f');
      ShowMessage(dataI +' data I');
      if ( dataF < dataI ) then
      begin
        ShowMessage('data F low');
      end
      else
      begin
        ShowMessage('data F high');
        F_inicio.Show;
      end;
    finally
      FreeAndNil(TextFile);
    end
  except
    // NEVER just "eat" an exception. Especially not while developing the
    // application.
    // Always either log the exception or show it to the user.
    on E:Exception do ShowMessage('Exception '+E.ClassName+': '+E.Message+#13#10+
                                  'need login');
  end;
end;

现在-如果执行此操作,将显示异常和错误消息。为了正确诊断错误,这是必需的。也许当您看到问题出在哪里时,您甚至可以自己解决这个问题...