美文网首页Flutter
flutter io 文件的操作

flutter io 文件的操作

作者: 基本密码宋 | 来源:发表于2019-05-17 17:57 被阅读0次
引入第三方插件
  • pubspec.yaml中 加入 插件的引用
dependencies:
 flutter:
   sdk: flutter
 path_provider: ^0.4.1
  • 终端加载插件
flutter packages get

最后在项目中引入

查看目录
临时目录

getTemporaryDirectory

文档目录

getApplicationDocumentsDirectory

sd卡目录

getExternalStorageDirectory

打印结果
  • 临时目录/data/data/com.jeno.flutter_github/cache
  • 文档目录/data/data/com.jeno.flutter_github/app_flutter
  • sd卡目录/storage/emulated/0
新增文件、读取文件
  /**
  * 加载文件
  * async 异步
  */
 localPath() async {
   try {
     //得到临时目录
     var tempDir = await getTemporaryDirectory();
     String tempPath = tempDir.path;

     //文档目录
     var appDorDir = await getApplicationDocumentsDirectory();
     String appDocPath = appDorDir.path;

     //sd卡目录
     var sdDir = await getExternalStorageDirectory();
     String sdPaht = sdDir.path;


     print('临时目录' + tempPath);
     print('文档目录' + appDocPath);
     print('sd卡目录' + sdPaht);

     ////文件的读写操作
     Directory(appDocPath + "/我的文件1/" + "txt文件").create(
         recursive: true).then((Directory d) {
       return new File(d.path + "/" + "1.txt").create(recursive: true).then((
           File file) {
         file.writeAsString('往缓存文件中加入数据2').then((File file) {
           //当写完数据后才能读取
           file.readAsString().then((String data) {
             print('数据是:$data');
           });
         });
       });
     });
   } catch (e) {
     print(e);
   }
 }
文件的删除
 //文件的删除操作
     Directory(appDocPath + "/我的文件1/" + "txt文件").delete(recursive: true).then((
         FileSystemEntity fileSystemEntity) {
       print('删除path' + fileSystemEntity.path);
     });
   } catch (e) {
     print(e);
   }

博客地址:https://jjmima.top/2019/05/17/flutter%20io%20%E6%96%87%E4%BB%B6%E7%9A%84%E6%93%8D%E4%BD%9C/
项目地址:https://github.com/songjiabin/flutter

相关文章

网友评论

    本文标题:flutter io 文件的操作

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