ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Flutter开发之——文件及文件夹操作,深入理解kotlin协程pdf

2021-12-24 16:58:52  阅读:351  来源: 互联网

标签:pathSeparator 协程 kotlin await Platform documentsDirectory file pdf path


_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} documentsDirectory.path{Platform.pathSeparator}dirName’;

Stream fileList = Directory(path).list();

await for(FileSystemEntity fileSystemEntity in fileList){

print(’$fileSystemEntity’);

}

}

说明:

  • Directory(path).list()中有一个可选参数recursive,默认值为false,表示只遍历当前目录;

  • 设置为true时表示遍历当前目录及子目录。

判断文件的类型:

await for(FileSystemEntity fileSystemEntity in fileList){

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

print(’$fileSystemEntity’);

FileSystemEntityType type = FileSystemEntity.typeSync(fileSystemEntity.path);

}

文件的类型:

  • file:文件

  • directory:文件夹

  • link:链接文件

  • notFound:未知

3.3 重命名文件夹名称

_dirRename() 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 dir3= await dir.rename(’ d i r . p a r e n t . a b s o l u t e . p a t h {dir.parent.absolute.path} dir.parent.absolute.path{Platform.pathSeparator}dir3’);

}

3.4 删除文件夹

_deleteDir() 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}dir3’;

var dir = await Directory(path).delete();

}

说明:

  • delete中有一个可选参数recursive,默认值为false,为false时如果删除的文件夹下还有内容将无法删除,抛出异常

  • 设置为true时,删除当前文件夹及文件夹下所有内容

四 文件的操作


4.1 创建一个 file.txt 文件

_createFile() 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${Platform.pathSeparator}file.txt’;

var file = await File(path).create(recursive: true);

}

说明:

  • create 中有一个可选参数 recursive,默认值为 false,为 false 时只创建文件,文件夹路径不存在抛出异常

  • 设置为 true 时,创建文件及不存在的路径文件夹

4.2 写入数据

_write2File() 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${Platform.pathSeparator}file.txt’;

var file=File(path);

if (file.existsSync()) {

file.writeAsString(‘写入数据文件’); //写入字符串

//file.writeAsBytes(Utf8Encoder().convert(“写入数据文件”));//写入 bytes 数据

//file.openWrite(mode: FileMode.append).write(‘追加到末尾’); //向末尾追加内容

}

}

4.3 读取数据

_readFile() 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${Platform.pathSeparator}file.txt’;

var file=File(path);

if (file.existsSync()) {

List lines = await file.readAsLines();

lines.forEach((element) {

print(’$element’);

});

}

}

4.4 删除文件

_deleteFile() 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${Platform.pathSeparator}file.txt’;

var file=File(path);

if (file.existsSync()) {

file.delete();

}

}

五 json文件数据读取


5.1 添加json文件数据

读取项目中文件,比如 asset/json/data.json 文件,data.json 文件中为 json 格式数据

[

{

“desc”: “开发环境搭建。”,

“title”: “第一章”

},

{

“desc”: “语法知识学习”,

“title”: “第二章”

},

{

“desc”: “组件学习”,

“title”: “第三章”

}

]

5.2 项目的 pubspec.yaml 文件中添加配置

assets:

  • assets/json/

5.3 读取json文件数据

_loadAsset(BuildContext context) async{

var jsonStr = await DefaultAssetBundle.of(context).loadString(‘assets/json/data.json’);

var list = json.decode(jsonStr);

print(list);

}

六 布局文件


6.1 代码

标签:pathSeparator,协程,kotlin,await,Platform,documentsDirectory,file,pdf,path
来源: https://blog.csdn.net/m0_65512512/article/details/122131406

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有