当前位置:网站首页>Flutter 文件读写-path_provider
Flutter 文件读写-path_provider
2022-06-29 06:48:00 【xiangxiongfly915】
文章目录
Flutter 文件读写-path_provider
概述
- 在我们操作文件的时候我们需要结合
dart:io库中的path_provider,因为每个系统下文件路径不同,如果自己去实现会很麻烦,那么path_provider就此产生。 path_provider是一个Flutter插件,主要作用是提供一种以平台无关一致的方式访问设备的文件系统,比如应用临时目录、文档目录等。而且path_provider支持Android、iOS、Linux、MacOS、Windows。
添加依赖
path_provider: ^2.0.11
支持平台
App存储目录总共分八种:

getTemporaryDirectory():临时目录- 特点:程序私有目录,其他应用无法访问,适用于缓存文件,可以被系统随时清空的。
- iOS的实现方式是
NSCachesDirectory - Android的实现方式是
getCacheDir
getApplicationSupportDirectory():应用程序支持目录- 特点:程序私有目录,其他应用无法访问,系统不会清除该目录,只有在删除应用时才会消失。
- iOS的实现方式是
NSApplicationSupportDirectory - Android的实现方式是
getFilesDir
getLibraryDirectory():应用程序持久文件目录- 特点:应用程序可以在其中存储持久性文件,备份文件以及对用户不可见的文件的目录路径,例如storage.sqlite.db。
getApplicationDocumentsDirectory():文档目录- 特点:用于存储只能由该应用访问的文件,系统不会清除该目录,只有在删除应用时才会消失。
- iOS的实现方式是
NSDocumentDirectory - Android的实现方式是
getDataDirectory或getExternalStorageDirectory
getExternalStorageDirectory():外部存储目录- 特点:用于获取外部存储目录,如SD卡等,但iOS不支持外部存储目录,目前只有Android才支持。
- Android的实现方式是
getExternalFilesDir
getExternalCacheDirectories():外部存储缓存目录- 特点:主要用户获取应用程序特定外部缓存数据的目录,比如从SD卡或者手机上有多个存储目录的,但iOS不支持外部存储目录,目前只有Android才支持。
- Android的实现方式是
getExternalCacheDirs或getExternalCacheDir
getExternalStorageDirectories():外部存储目录(单独分区)- 特点:可根据类型获取外部存储目录,如SD卡、单独分区等,和外部存储目录不同在于他是获取一个目录数组。但iOS不支持外部存储目录,目前只有Android才支持。
- Android的实现方式是
getExternalFilesDirs或getExternalFilesDir
getDownloadsDirectory():桌面程序下载目录- 特点:主要用于存储下载文件的目录,只适用于
Linux、MacOS、Windows,Android和iOS平台无法使用。
- 特点:主要用于存储下载文件的目录,只适用于
平时我们常用三种:getApplicationDocumentsDirectory()、getTemporaryDirectory()、getApplicationSupportDirectory()。
使用path_provider

class PathProviderDemo extends StatefulWidget {
const PathProviderDemo({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() {
return _PathProviderDemoState();
}
}
class _PathProviderDemoState extends State<PathProviderDemo> {
late Future<Directory> _tempDirectory;
late Future<Directory> _appSupportDirectory;
late Future<Directory> _appLibraryDirectory;
late Future<Directory> _appDocumentsDirectory;
late Future<Directory?> _externalStorageDirectory;
late Future<List<Directory>?> _externalStorageDirectories;
late Future<List<Directory>?> _externalCacheDirectories;
late Future<Directory?> _downloadDirectory;
@override
void initState() {
super.initState();
setState(() {
_tempDirectory = getTemporaryDirectory();
_appSupportDirectory = getApplicationSupportDirectory();
_appLibraryDirectory = getLibraryDirectory();
_appDocumentsDirectory = getApplicationDocumentsDirectory();
_externalStorageDirectory = getExternalStorageDirectory();
_externalCacheDirectories = getExternalCacheDirectories();
_externalStorageDirectories = getExternalStorageDirectories();
_downloadDirectory = getDownloadsDirectory();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("path_provider支持平台"),
),
body: ListView(
children: [
_buildItem("getTemporaryDirectory", _tempDirectory),
_buildItem("getApplicationSupportDirectory", _appSupportDirectory),
_buildItem("getLibraryDirectory", _appLibraryDirectory),
_buildItem("getApplicationDocumentsDirectory", _appDocumentsDirectory),
_buildItem("getExternalStorageDirectory", _externalStorageDirectory),
_buildItem2("getExternalCacheDirectories", _externalCacheDirectories),
_buildItem2("getExternalStorageDirectories", _externalStorageDirectories),
_buildItem("getDownloadsDirectory", _downloadDirectory),
],
),
);
}
Widget _buildItem(String title, Future<Directory?> future) {
return Column(
children: [
Text("$title:"),
FutureBuilder(future: future, builder: _buildDirectory),
const Divider(),
],
);
}
Widget _buildItem2(String title, Future<List<Directory>?> future) {
return Column(
children: [
Text("$title:"),
FutureBuilder(future: future, builder: _buildDirectories),
const Divider(),
],
);
}
Widget _buildDirectory(BuildContext context, AsyncSnapshot<Directory?> snapshot) {
Text text = const Text("");
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
text = Text("Error: ${snapshot.error}");
} else if (snapshot.hasData) {
text = Text("path: ${snapshot.data?.path}");
} else {
text = const Text("不支持");
}
}
return text;
}
Widget _buildDirectories(BuildContext context, AsyncSnapshot<List<Directory>?> snapshot) {
Text text = const Text("2");
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
text = Text("Error: ${snapshot.error}");
} else if (snapshot.hasData) {
final String? paths = snapshot.data?.map((Directory d) => d.path).join(",");
text = Text("paths: $paths");
} else {
text = const Text("不支持");
}
}
return text;
}
}
操作文件夹&文件
创建文件夹
/// 创建文件夹
createDir() async {
final parentDir = await getApplicationSupportDirectory();
String path = "${parentDir.path}${Platform.pathSeparator}hello/a/b/c";
print("path:$path");
var dir = Directory(path);
bool exist = dir.existsSync();
if (exist) {
print("文件已存在");
} else {
var result = dir.create(recursive: true);
print("创建$result");
}
}
Platform.pathSeparator:平台分隔符,在Android和iOS中表示“/”。create():创建文件夹,默认不支持嵌套文件夹,create(recursive:true)支持嵌套文件夹的创建。
遍历文件夹
/// 遍历文件夹中的文件
queryDir() async {
final parentDir = await getApplicationSupportDirectory();
String path = "${parentDir.path}${Platform.pathSeparator}hello";
Stream<FileSystemEntity> fileList = Directory(path).list(recursive: true);
fileList.forEach((element) {
FileSystemEntityType type = FileSystemEntity.typeSync(element.path);
print("$element 类型:$type");
});
}
list():默认值为false,只遍历当前目录;为true时,遍历当前目录和子目录。- 文件类型:
- file:文件
- directory:文件夹
- link:链接文件
- notFound:未知
文件重命名
/// 重命名文件夹
renameDir() async {
final parentDir = await getApplicationSupportDirectory();
String path = "${parentDir.path}${Platform.pathSeparator}hello";
var dir = Directory(path);
var newDir = await dir.rename("${parentDir.path}${Platform.pathSeparator}newhello");
print(newDir);
}
删除文件夹
/// 删除文件夹
deleteDir() async {
final parentDir = await getApplicationSupportDirectory();
String path = "${parentDir.path}${Platform.pathSeparator}hello";
var dir = await Directory(path).delete(recursive: true);
print(dir);
}
delete():删除文件夹,默认不支持嵌套文件夹,create(recursive:true)支持嵌套文件夹的删除。
创建文件
/// 创建文件
createFile() async {
final parentDir = await getApplicationSupportDirectory();
String path = "${parentDir.path}${Platform.pathSeparator}mytxt.txt";
var file = File(path);
bool exist = file.existsSync();
if (exist) {
print("文件已存在");
} else {
await file.create();
print(file);
}
}
删除文件
file.delete();
文件写入数据
/// 文件写入数据
writeFile() async {
final parentDir = await getApplicationSupportDirectory();
String path = "${parentDir.path}${Platform.pathSeparator}mytxt.txt";
var file = File(path);
await file.writeAsString("哈喽 flutter1");
//追加写入
await file.writeAsBytes(const Utf8Encoder().convert("\n哈喽 flutter2"), mode: FileMode.writeOnlyAppend);
}
文件读取数据
/// 文件读取数据
readFile() async {
final parentDir = await getApplicationSupportDirectory();
String path = "${parentDir.path}${Platform.pathSeparator}mytxt.txt";
var file = File(path);
List<String> lines = await file.readAsLines();
for (var e in lines) {
print(e);
}
//读取bytes并转String
String result = const Utf8Decoder().convert(await file.readAsBytes());
print("result: $result");
}
读取asset文件
/// 读取asset文件
readAsset(BuildContext context) async {
//方式一
String jsonStr = await DefaultAssetBundle.of(context).loadString("assets/students.json");
print("jsonStr: $jsonStr");
var list = json.decode(jsonStr);
for (var e in list) {
print(e);
}
//方式二
String jsonStr2 = await rootBundle.loadString("assets/students.json");
print("jsonStr2: $jsonStr2");
}
边栏推荐
- Manipulate the outer DOM in the iframe tag
- AI and the meta universe sparked a spark: human beings lost only shackles and gained all-round liberation
- Selected Siemens PLC project example source code [300 sets in total]
- JS XOR obfuscation code
- 【深度之眼吴恩达机器学习作业班第四期】Regularization正则化总结
- Line features & surface features of vSLAM features
- How to share the virtual environment of pycharm to jupyter Lab
- Automated test - uiautomator2 framework application - automatic clock in
- 手把手系列---安装SpotBugs、并快速上手使用
- Thread pool operations in cartographer
猜你喜欢

MySQL中有哪些约束?(实例验证)

AI deep dive of Huawei cloud

Mongodb- connect to the database using the mongo/mongosh command line

Reasons why the ext.dic file configured in ES does not take effect

Un voyage profond d'IA dans Huawei Cloud

基于Sophus的Ceres优化

【深度之眼吴恩达机器学习作业班第四期】Logistic Regression 逻辑回归总结

Thread pool operations in cartographer

华为云的AI深潜之旅

手把手系列---安装SpotBugs、并快速上手使用
随机推荐
Compiling principle: the king's way
软重启(reboot)
在colaboratory上云端使用GPU训练(以YOLOv5举例)
PostgreSQL installation: the database cluster initialization failed, stack hbuilder installation
RobotFramework学习笔记:Robot Framework和BrowserLibrary(PlayWright)简介
小白大战指针 (上)
在iframe标签中操作外层dom
NOR flash application layer operation
【深度之眼吴恩达机器学习作业班第四期】Logistic Regression 逻辑回归总结
【kerberos】kerberos 认证浅析
SQL Server 开启cdc
華為雲的AI深潜之旅
Reflection perfectionism
【量化投资系统】因子处理安装talib
Django - installing mysqlclient error: mysqlclient 1.4.0 or newer is required; you have 0.9.3
Soft restart
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
音视频开发案例99讲-目录
C mqtt subscription message
SizeBalanceTree