代码之家  ›  专栏  ›  技术社区  ›  rawcane

将FileEntities从Directory.list()转换为字符串时出现问题

  •  0
  • rawcane  · 技术社区  · 7 月前

    如果我有这样的剧本

    import 'package:path/path.dart' as p;
    import 'dart:io';
    import 'dart:async';
    
    void main() async {
      final inDir = p.join(Directory.current.path, 'tmp', 'in'); // should we rename this
      var dir = Directory(inDir);
      final List<FileSystemEntity> fileList = await dir.list().toList();
      for (var file in fileList.where((entity) => entity is File) ) {
        print ("Processing ${(file as File).toString()}");
        final contents = (file as File).readAsStringSync();
        print (contents);
      }
    }
    

    它工作得很好。

    Processing File: 'C:\FlutterProjects\project\tmp\a.txt'
    some text
    

    但是,如果我想将文件路径作为字符串传递给另一个函数

    import 'package:path/path.dart' as p;
    import 'dart:io';
    import 'dart:async';
    
    void main() async {
      final inDir = p.join(Directory.current.path, 'tmp'); // should we rename this
      var dir = Directory(inDir);
      final List<FileSystemEntity> fileList = await dir.list().toList();
      for (var file in fileList.where((entity) => entity is File) ) {
        print ("Processing ${(file as File).toString()}");
        readFile ((file as File).toString());
      }
    }
    
    void readFile (String filePath) {
    
        final file = File(filePath); 
        final contents = file.readAsStringSync();
        print (contents);
    
    }
    

    …那么它就不起作用了。。。

    Processing File: 'C:\FlutterProjects\project\tmp\a.txt'
    Unhandled exception:
    FileSystemException: Cannot open file, path = 'File: 'C:\FlutterProjects\project\tmp\a.txt'' (OS Error: The filename, directory name, or volume label syntax is incorrect.
    , errno = 123)
    #0      _File.throwIfError (dart:io/file_impl.dart:675:7)
    #1      _File.openSync (dart:io/file_impl.dart:490:5)
    #2      _File.readAsBytesSync (dart:io/file_impl.dart:574:18)
    #3      _File.readAsStringSync (dart:io/file_impl.dart:624:18)
    #4      readFile (file:///C:/FlutterProjects/project/bin/parse-files.dart:18:27)
    #5      main (file:///C:/FlutterProjects/project/bin/parse-files.dart:11:5)
    <asynchronous suspension>
    
    

    我猜是因为它一开始还有文件:。因此,我可以将其视为函数本身中的一个实体,或者我将字符串转换为没有File:a作为开头,但我不明白为什么它没有将文件的字符串作为文件发送(这就是我想做的,让函数更具可重用性)。

    TL;DR为什么不 ((file as File).toString()); 当文件是FileEntity时,执行我所期望的操作。

    1 回复  |  直到 7 月前
        1
  •  1
  •   julemand101    7 月前

    调用的输出 toString() File对象上的路径不是可用于某些内容的路径。如果你看到你的代码,调用的结果 toString() 在你的 File 实际上是字符串:

    File: 'C:\FlutterProjects\project\tmp\a.txt'
    

    所以,你最终要做的是:

    File("File: 'C:\FlutterProjects\project\tmp\a.txt'");
    

    这会失败,因为这不是一个有效的路径,也不是你可以读取的内容。

    而不是打电话 toString() ,你应该使用 .path 财产在 文件 对象。或者直接发送 文件 作为方法的论据:

    import 'package:path/path.dart' as p;
    import 'dart:io';
    
    void main() async {
      final inDir = p.join(Directory.current.path, 'tmp'); // should we rename this
      var dir = Directory(inDir);
      final List<FileSystemEntity> fileList = await dir.list().toList();
      
      for (var file in fileList.whereType<File>()) {
        print("Processing ${file.path}");
        readFile(file);
      }
    }
    
    void readFile(File file) {
      final contents = file.readAsStringSync();
      print(contents);
    }
    

    此外,我还修复了此代码中不必要的文件类型强制转换问题。如果你使用 whereType ,它实际上基于类型检查进行过滤,并返回正确类型的对象。