简体   繁体   English

Flutter中如何读写一个文本文件

[英]How to read and write a text file in Flutter

How do you read text from a file and write text to a file?如何从文件中读取文本并将文本写入文件?

I've been learning about how to read and write text to and from a file.我一直在学习如何在文件中读写文本。 I found another question about reading from assets , but that is not the same.我发现了另一个关于reading from assets的问题,但那是不一样的。 I will add my answer below from what I learned from the documentation.我将根据我从文档中学到的知识在下面添加我的答案。

Setup设置

Add the following plugin in pubspec.yaml :pubspec.yaml添加以下插件:

dependencies:
  path_provider: ^1.6.27

Update the version number to whatever is current .将版本号更新为当前的任何版本。

And import it in your code.并将其导入到您的代码中。

import 'package:path_provider/path_provider.dart';

You also have to import dart:io to use the File class.您还必须导入dart:io才能使用File类。

import 'dart:io';

Writing to a text file写入文本文件

_write(String text) async {
  final Directory directory = await getApplicationDocumentsDirectory();
  final File file = File('${directory.path}/my_file.txt');
  await file.writeAsString(text);
}

Reading from a text file从文本文件中读取

Future<String> _read() async {
  String text;
  try {
    final Directory directory = await getApplicationDocumentsDirectory();
    final File file = File('${directory.path}/my_file.txt');
    text = await file.readAsString();
  } catch (e) {
    print("Couldn't read file");
  }
  return text;
}

Notes笔记

  • You can also get the path string with join(directory.path, 'my_file.txt') but you need to import 'package:path/path.dart' .您还可以使用join(directory.path, 'my_file.txt')获取路径字符串,但您需要导入'package:path/path.dart'
  • Flutter's Official Documentation of Reading and Writing Files Flutter 官方文档读写文件
  • This works for iOS, Android, Linux and MacOS but not for web.这适用于 iOS、Android、Linux 和 MacOS,但不适用于 Web。

As additional info to @Suragch's answer, if you want to find the file you created, you can do as the images show:作为@Suragch 答案的附加信息,如果您想找到您创建的文件,您可以按照图片所示进行操作:

在此处输入图片说明

And then inside that data folder, go again to a folder named data and search for your package, and then go to:然后在该数据文件夹中,再次转到名为 data 的文件夹并搜索您的包,然后转到:

在此处输入图片说明

If you happen to create new files, in order to be able to see them, just right click and click Synchronize .如果您碰巧创建了新文件,为了能够看到它们,只需右键单击并单击Synchronize

在此处输入图片说明

An another way to pull the file from the device is by using adb pull command.从设备中拉取文件的另一种方法是使用 adb pull 命令。 You can find the file path by debugging the code and then use adb pull command.您可以通过调试代码找到文件路径,然后使用 adb pull 命令。 adb is located in Android SDK -> platform-tools directory. adb 位于 Android SDK -> platform-tools 目录。

./adb pull /storage/emulated/0/Android/data/com.innovate.storage.storage_sample/files/sample.txt ~/Downloads

@Suragch 's answer is right. @Suragch 的回答是正确的。 Except the version of path_provider that you want to use now is:除了您现在要使用的path_provider版本是:

path_provider: ^2.0.9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM