}
}
说明:
-
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}
documentsDirectory.path{Platform.pathSeparator}dirName’;
Stream fileList = Directory(path).list();
await for(FileSystemEntity fileSystemEntity in fileList){
print(’$fileSystemEntity’);
}
}
说明:
判断文件的类型:
await for(FileSystemEntity fileSystemEntity in fileList){
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();
}
说明:
四 文件的操作
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);
}
说明:
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:
5.3 读取json文件数据
|