如何解决flutter中读取文件时出现错误(操作系统错误:没有这样的文件或目录,errno = 2)

问题描述 投票:0回答:3

我最近开始使用 flutter 编写移动应用程序。我在读取文件时遇到问题。在实践中,当应用程序运行时,我将一个字符串写入文本文件(并且我也可以正确读取它)。我的需要是每次访问应用程序时都从该文件中读取;因此,在 initState () 函数中,我调用 readContent () 函数,但收到以下消息:“FileSystemException: 无法打开文件,路径 = '/var/mobile/Containers/Data/Application/F8A5A202-45C9-47F6-93C9 -E4BAE2AF3C7E/Documents/counter.txt'(操作系统错误:没有这样的文件或目录,errno = 2)”。你能帮我吗?

这是我的代码:

Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();
    print("directory path: "+directory.path);
    return directory.path;
  }

  Future<io.File> get _localFile async {
    final path = await _localPath;
    return io.File('$path/counter.txt');
  }

  String datas = "";

  //questa funzione viene richiamata all'avvio della schermata e serve per leggere il valore del flag checkSendingReport dal file
  @override
  void initState() {
    super.initState();
    readContent().then((String value) {
      print(value);
      setState(() {
        String data = value;

        List data_file_split = data.split("#");
        checkSendingReport = data_file_split[1];
        check_sending_report = data_file_split[1];
      });
    });
  }

  //Questa funzione serve per leggere il file locale scritto in precenza
  Future<String> readContent() async {
    try {
      final file = await _localFile;

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

    } catch (e) {
      // If encountering an error, return
      return 'Error! '+e.toString();
    }
  }

  //questa funzione serve per scrivere il file locale
  Future<io.File> writeContent(datetime) async {
    final file = await _localFile;

    //Setto a true la variabile flag check_sending_report
    check_sending_report = true;

    //Creo la stringa da inserire nel file: composta da datetime#flag
    String data_file = datetime + "#" + check_sending_report.toString();

    // Write the file
    return file.writeAsString(data_file);
  }
ios flutter dart readfile writefile
3个回答
2
投票

我猜这个错误是由于最初的文件不存在而发生的。 因此,您可以添加文件是否存在的检查

if(file.existsSync())

如果文件不存在,您可以创建一个。

new File('$path/counter.txt').create(recursive: true);

1
投票

这个答案由我完成

 Future<File>  _getLocalFile(String pathFlie) async {
      final root = await getApplicationDocumentsDirectory();
      final path = root+'/'+pathFile;
      return File(path).create(recursive: true);
 }

0
投票
  final root = await getApplicationDocumentsDirectory();
  print(root.path + '/logFile.txt');
  var filePath = '${root.path}/logFile.txt';
  var logFile = File(filePath);
  if (!logFile.existsSync()) {
    logFile.create(recursive: true);
  }
  logFile.writeAsStringSync('${record.time}: ${record.message}\n',
      mode: FileMode.append);
  logFile.readAsString().then((String contents) {
    print("contents is $contents");
  });
  final result = await OpenFile.open(filePath);
  print(result.toString());
 
© www.soinside.com 2019 - 2024. All rights reserved.