如何在运行时清除浮动文本文件? -飞镖/扑

时间:2020-08-07 03:22:50

标签: flutter dart

在写入文本文件之前,有什么方法可以完全清除该文本文件?

为使问题更清楚,我创建的类如下所示:(请原谅下面用于方法/类/参数的令人困惑的名称)。


class ReadWriteUrl {
  Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();

    return directory.path;
  }

  Future<File> get _localFile async {
    final path = await _localPath;

    return File('$path/Url.txt');
  }

  Future<String> readCounter() async {
    try {
      final file = await _localFile;

      // Read the file
      String contents = await file.readAsString();

      return contents;
    } catch (e) {
      return "null";
    }
  }

  Future<File> writeCounter(String counter) async {
    final file = await _localFile;

    // Write the file
    return file.writeAsString('$counter');
  }
}

但是,每次我尝试写任何文本时,它不能完全清除文本。 例如,当我调用以下方法时,假设文本文件在第1行包含“ Hello World”,在第2行包含“ Hello Everyone”

writeCounter("Hi")

第1行的输出为“ Hillo World”,第2行的输出为“ Hello Everyone”。 但是,我只希望“ Hi”出现在整个文本文件中。有没有办法删除第1行和第2行,即在我打电话之前清除文本文件 writeCounter(“ Hi”)方法。

0 个答案:

没有答案