参考 参考:老孟 文件存储和网络请求 数据存储 dart的 io 库包含了文件读写的相关类,它属于 Dart 语法标准的一部分,所以通过 Dart IO 库,无论是 Dart VM 下的脚本还是 Fl
参考:老孟 文件存储和网络请求
dart的 io 库包含了文件读写的相关类,它属于 Dart 语法标准的一部分,所以通过 Dart IO 库,无论是 Dart VM 下的脚本还是 Flutter,都是通过 Dart IO 库来操作文件的。但是不同的平台对应的文件系统是不同的,比如文件路径,因此通常使用Google 官方维护的插件 path_provider。
在项目的 pubspec.yaml 文件中添加依赖
dependencies: path_provider: ^2.0.11 安装
flutter pub get getTemporaryDirectory
临时目录,适用于下载的缓存文件,此目录随时可以清除,此目录为应用程序私有目录,其他应用程序无法访问此目录。
getApplicationSupportDirectory
获取应用程序的支持目录。这个函数返回一个Future对象,表示应用程序的支持目录。
getLibraryDirectory
返回应用程序的库目录。这个目录通常用于存储应用程序的共享库或插件。
getApplicationDocumentsDirectory
应用程序的文档目录。文档目录是应用程序可以使用的持久性存储空间,通常用于存储用户生成的文件或其他数据。
getExternalStorageDirectory
应用程序可以访问顶级存储的目录的路径。由于此功能仅在Android上可用,因此应在发出此函数调用之前确定当前操作系统。
getExternalCacheDirectories
存储特定于应用程序的外部缓存数据的目录的路径。
getDownloadsDirectory
存储下载文件的目录的路径,这通常仅与台式机操作系统有关。 在Android和iOS上,此函数将引发[UnsupportedError]异常。
下面从 Android 和 iOS 平台的角度介绍其文件路径,最后给出路径使用的建议以及使用过程中需要注意的事项。
Android 文件存储分为内部存储和外部存储。
内部存储:
内部存储目录结构:
外部存储
外部存储目录结构
iOS 文件存储相比 Android 要简单的多,因为 iOS 对用户隐私保护非常严格,每个 iOS 应用程序都有一个单独的文件系统,而且只能在对应的文件系统中进行操作,此区域被称为沙盒。
每个应用沙盒含有3个文件夹:Documents, Library 和 tmp:
Android/data/包名/ ,对应 getExternalCacheDirectories 和 getExternalStorageDirectories 方法。创建文件夹
ElevatedButton(onPressed: () async{ // 获取应用程序目录 Directory appDocumentDriectory = await getApplicationDocumentsDirectory(); // 路径,PlatfORM.pathSeparator 平台下的路径分隔符 String path = '${appDocumentDriectory.path}${Platform.pathSeparator}myAppName'; // 读取对应路径下的文件夹 var dir = Directory(path); if(dir.existsSync()){ print("当前文件夹已存在"); }else{ // 创建文件,可选参数recursive:true表示可以创建嵌套文件夹,false表示只能创建最后一级文件夹(上一级文件不存在会报错),默认false var result = await dir.create(recursive: true); print("文件创建成功:${result}"); } }, child: const Text("创建文件夹")); 
遍历文件夹
ElevatedButton(onPressed: () async{ // 获取应用程序目录 Directory appDocumentDirectory = await getApplicationDocumentsDirectory(); String path = '${appDocumentDirectory.path}${Platform.pathSeparator}'; // 文件列表,可选参数recursive,默认值为false,只遍历当前目录;设置为true时会遍历当前目录及子目录 Stream<FileSystemEntity> fileList = Directory(path).list(); await for(FileSystemEntity fileSystemEntity in fileList){ print(fileSystemEntity); }}, child: const Text("遍历文件")) 
判断文件的类型
await for(FileSystemEntity fileSystemEntity in fileList){ print('$fileSystemEntity'); FileSystemEntityType type = FileSystemEntity.typeSync(fileSystemEntity.path);} 文件的类型:
文件夹重命名
_dirRename() async{ Directory documentsDirectory = await getApplicationDocumentsDirectory(); String path = '${documentsDirectory.path}${Platform.pathSeparator}dirName'; var dir = Directory(path); var newName= await dir.rename('${dir.parent.absolute.path}${Platform.pathSeparator}newName');} 删除文件夹
ElevatedButton(onPressed: () async{ // 获取应用程序文件目录 Directory appDocumentDirectory = await getApplicationDocumentsDirectory(); String path ='${appDocumentDirectory.path}${Platform.pathSeparator}myAppName'; try{ // 可选参数recursive,默认false,只删除文件夹,如果文件夹下有内容则无法删除并抛出异常;true删除文件夹及文件夹下所有内容 var dir = await Directory(path).delete(); print('文件夹$path删除成功'); }catch(err){ print('文件夹$path删除失败:$err'); } }, child: const Text("删除文件夹")) 
创建文件
ElevatedButton(onPressed: () async{ // 获取应用程序目录 Directory appDocumentDirectory = await getApplicationDocumentsDirectory(); // 路径 String path = '${appDocumentDirectory.path}${Platform.pathSeparator}myAppName${Platform.pathSeparator}test.txt'; File file = File(path); if(file.existsSync()){ print("文件已存在"); }else{ // 文件创建也存在recursive属性 var file = await File(path).create(); print("文件创建成功:$file"); }}, child: const Text("创建文件")) 
写入数据
写入字符串,覆盖写入
ElevatedButton(onPressed: () async{ // 获取应用程序目录 Directory appDocumentDirectory = await getApplicationDocumentsDirectory(); // 路径 String path = '${appDocumentDirectory.path}${Platform.pathSeparator}myAppName${Platform.pathSeparator}test.txt'; File file = File(path); if(file.existsSync()){ var res = await file.writeAsString("写入数据"); print("写入成功:$res"); }else{ print("文件不存在"); }}, child: const Text("写入数据")), 
写入bytes 数据,覆盖写入
file.writeAsBytes(Utf8Encoder().convert("bytes 格式")); 追加写入
file.openWrite(mode: FileMode.append).write('追加写入'); 读取数据
读取字符串
ElevatedButton( onPressed: () async { // 应用程序目录 Directory appDocumentDirectory = await getApplicationDocumentsDirectory(); // 路径 String path = '${appDocumentDirectory.path}${Platform.pathSeparator}myAppName${Platform.pathSeparator}test.txt'; File file = File(path); if (file.existsSync()) { var res = await file.readAsString(encoding: utf8); print("文件读取成功,内容是:$res"); } else { print("文件不存在"); } }, child: const Text("文件读取")) 
按行读取数据
List<String> lines = await file.readAsLines();lines.forEach((element) { print('$element');}); 读取 bytes 并转换为String
Utf8Decoder().convert(await file.readAsBytes()); 删除
file.delete(); 读取项目中文件,项目中使用的文件一般都放在lib/assets下

如果需要访问,需要在pubspec.yaml 文件中添加配置,例如:
assets: - assets/json/ 读取
_loadAsset(BuildContext context) async{ var jsonStr = await DefaultAssetBundle.of(context) .loadString('assets/json/data.json'); var list = json.decode(jsonStr); list.forEach((element) { print('$element'); });} 来源地址:https://blog.csdn.net/weixin_41897680/article/details/131167527
--结束END--
本文标题: flutter:文件系统目录、文件读写
本文链接: https://www.lsjlt.com/news/485668.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0