文件操作

创建文件夹
Directory documentsDirectory = await getApplicationDocumentsDirectory();
            String path = '${documentsDirectory.path}${Platform.pathSeparator}dirName';
            var dir = Directory(path);
            var exist = dir.existsSync();
            if (exist) {
              print('当前文件夹已经存在');
            } else {
              var result = await dir.create();//create(recursive: true)支持"/aa/bb嵌套"
              print('$result');
            }
            遍历目录
 Directory documentsDirectory = await getApplicationDocumentsDirectory();
            String path = '${documentsDirectory.path}';
            Stream<FileSystemEntity> fileList = Directory(path).list();
            await for(FileSystemEntity fileSystemEntity in fileList){
              print('$fileSystemEntity');
            }
            重命名
            Directory documentsDirectory = await getApplicationDocumentsDirectory();
  String path = '${documentsDirectory.path}${Platform.pathSeparator}dirName';
  var dir = Directory(path);
  var dir3= await dir.rename('${dir.parent.absolute.path}${Platform.pathSeparator}dir3');
  删除
  var dir = await Directory(path).delete();//recursive: true可删除当前文件夹和下面内容
  创建文件
  Directory documentsDirectory = await getApplicationDocumentsDirectory();
  String path = '${documentsDirectory.path}${Platform.pathSeparator}dirName${Platform.pathSeparator}file.txt';

  var file = await File(path).create(recursive: true);//路径文件一起创建
  写入字符
  file.writeAsString('老孟 Flutter');
  写入字节
  file.writeAsBytes(Utf8Encoder().convert("老孟 Flutter bytes 格式"));
  一行一行读取
  List<String> lines = await file.readAsLines();
lines.forEach((element) {
  print('$element');
});
删除文件
file.delete();
读取资源下
var jsonStr = await DefaultAssetBundle.of(context).loadString('assets/json/data.json');
文档更新时间: 2022-11-16 14:09   作者:Yoby