美文网首页Flutter开发圈
Flutter Flie本地文件读写

Flutter Flie本地文件读写

作者: 菜鸟何时起飞 | 来源:发表于2019-06-15 23:49 被阅读0次

    获取临时存储路径

     void _requestTempDirectory() async {
        Directory tempDir = await getTemporaryDirectory();
        setState(() {
          tempPath = tempDir.path;
        });
      }
    

    往文件写数据

      _add() async {
    
        File file = new File('$tempPath/user.txt');
        await file.writeAsString('用户名:$username\n密码:$pwd');
        setState(() {
          _result = '写入成功,请查询';
        });
      }
    

    读取文件数据

     _query() async {
        try {
          File file = new File('$tempPath/user.txt');
          _result = '查询成功\n' + await file.readAsString();
        } on Exception catch (e) {
          _result = ' exception: $e';
        }
    
        setState(() {});
      }
    

    删除

      _delete() {
        File file = new File('$tempPath/user.txt');
        file.deleteSync(recursive: false);
        setState(() {
          _result = '删除成功,请查看';
        });
      }
    

    相关文章

      网友评论

        本文标题:Flutter Flie本地文件读写

        本文链接:https://www.haomeiwen.com/subject/ntxwfctx.html