Flutter开发之——文件及文件夹操作,农民工看完都会了

  • 文件操作(创建、数据写入、读取数据、删除文件)

  • json文件数据读取

二 添加依赖


因为文件及文件夹的操作依赖于path_provider

在项目的 pubspec.yaml 文件中添加依赖

flutter pub add path_provider

点击pubspec.yaml右上角的Pub get或者执行如下指令

flutter pub get

三 文件夹操作(创建、遍历、重命名、删除)


3.1 创建文件夹

_createDir() async {

Directory documentsDirectory = await getApplicationDocumentsDirectory();

String path = ‘ d o c u m e n t s D i r e c t o r y . p a t h {documentsDirectory.path} documentsDirectory.path{Platform.pathSeparator}dirName’;

var dir = Directory(path);

var exist = dir.existsSync();

if (exist) {

print(‘当前文件夹已经存在’);

} else {

var result = await dir.create();

print(’$result’);

}

}

说明:

  • Platform.pathSeparator表示路径分隔符,对于Android和iOS来说表示‘/’

  • create 中有一个可选参数 recursive ,默认值为 false,false 表示只能创建最后一级文件夹

  • 如果创建 “dir1/dir2” 这种嵌套文件夹,recursive为 false 时将抛出异常,设置为 true 可以创建嵌套文件夹

3.2 遍历文件夹下文件

_dirList() async {

Directory documentsDirectory = await getApplicationDocumentsDirectory();

String path = ‘ d o c u m e n t s D i r e c t o r y . p a t h {documentsDirectory.path} document

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值