读写文件时如何解决“FileSystemException:无法打开文件”的问题?

Ale*_*rro 5 file download dart flutter

我对 dart/flutter 很陌生,所以如果有什么我可以澄清的,请告诉我。我正在尝试下载文件,但是当我尝试访问它时,出现错误“FileSystemException:无法打开文件,路径 = './data.csv'(操作系统错误:只读文件系统,errno = 30 )”我做错了什么,我该如何解决?

 downloadTextFile() {
    HttpClient client = new HttpClient();
    client.getUrl(Uri.parse(*I put the link I used here, but it's sort of long*))
        .then((HttpClientRequest request) {
      return request.close();
    })
        .then((HttpClientResponse response) {
      response.pipe(new File('./data.csv').openWrite());
    });
    readFileByLines();
  }

  void readFileByLines() {
    File file = new File('./data.csv');
    List<String> lines = file.readAsLinesSync();
    lines.forEach((l) => print(l));
  }
Run Code Online (Sandbox Code Playgroud)

chu*_*han 5

您可以使用包https://pub.dev/packages/path_provider
并将您的文件写入tempPathappDocPath

Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;

Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
Run Code Online (Sandbox Code Playgroud)

对于 Android , In AndroidManifest.xml,您可以添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)