_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();
}
}
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);
}