gpt4 book ai didi

dart - 将字符串列表写入文件

转载 作者:行者123 更新时间:2023-12-03 03:11:05 25 4
gpt4 key购买 nike

API page,我了解到我要尝试执行的功能没有任何功能。我想从文件中读取文本,并将其存储为字符串列表,操作文本并保存文件。第一部分很容易使用该函数:

abstract List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8])

但是,没有函数让我直接将列表的内容写到文件中,例如
abstract void writeAsLinesSync(List<String> contents, [Encoding encoding = Encoding.UTF_8, FileMode mode = FileMode.WRITE])

相反,我一直在使用:
abstract void writeAsStringSync(String contents, [Encoding encoding = Encoding.UTF_8, FileMode mode = FileMode.WRITE])

通过将列表简化为单个字符串。我确定我也可以使用for循环并逐行馈入流。我想知道两件事:
  • 有没有一种方法可以仅将文件交给要写入的字符串列表?
  • 为什么会有readAsLinesSync但没有writeAsLinesSync?这是疏忽还是设计决定?

  • 谢谢

    最佳答案

    我刚刚创建了自己的导出类,用于处理对文件的写入或用于将数据发送到websocket。

    用法:

    exportToWeb(mapOrList, 'local', 8080);
    exportToFile(mapOrList, 'local/data/data.txt');

    类:
    //Save data to a file.
    void exportToFile(var data, String filename) =>
    new _Export(data).toFile(filename);

    //Send data to a websocket.
    void exportToWeb(var data, String host, int port) =>
    new _Export(data).toWeb(host, port);

    class _Export {

    HashMap mapData;
    List listData;
    bool isMap = false;
    bool isComplex = false;

    _Export(var data) {
    // Check is input is List of Map data structure.
    if (data.runtimeType == HashMap) {
    isMap = true;
    mapData = data;
    } else if (data.runtimeType == List) {
    listData = data;
    if (data.every((element) => element is Complex)) {
    isComplex = true;
    }
    } else {
    throw new ArgumentError("input data is not valid.");
    }
    }

    // Save to a file using an IOSink. Handles Map, List and List<Complex>.
    void toFile(String filename) {
    List<String> tokens = filename.split(new RegExp(r'\.(?=[^.]+$)'));
    if (tokens.length == 1) tokens.add('txt');
    if (isMap) {
    mapData.forEach((k, v) {
    File fileHandle = new File('${tokens[0]}_k$k.${tokens[1]}');
    IOSink dataFile = fileHandle.openWrite();
    for (var i = 0; i < mapData[k].length; i++) {
    dataFile.write('${mapData[k][i].real}\t'
    '${mapData[k][i].imag}\n');
    }
    dataFile.close();
    });
    } else {
    File fileHandle = new File('${tokens[0]}_data.${tokens[1]}');
    IOSink dataFile = fileHandle.openWrite();
    if (isComplex) {
    for (var i = 0; i < listData.length; i++) {
    listData[i] = listData[i].cround2;
    dataFile.write("${listData[i].real}\t${listData[i].imag}\n");
    }
    } else {
    for (var i = 0; i < listData.length; i++) {
    dataFile.write('${listData[i]}\n');
    }
    }
    dataFile.close();
    }
    }

    // Set up a websocket to send data to a client.
    void toWeb(String host, int port) {
    //connect with ws://localhost:8080/ws
    //for echo - http://www.websocket.org/echo.html
    if (host == 'local') host = '127.0.0.1';
    HttpServer.bind(host, port).then((server) {
    server.transform(new WebSocketTransformer()).listen((WebSocket webSocket) {
    webSocket.listen((message) {
    var msg = json.parse(message);
    print("Received the following message: \n"
    "${msg["request"]}\n${msg["date"]}");
    if (isMap) {
    webSocket.send(json.stringify(mapData));
    } else {
    if (isComplex) {
    List real = new List(listData.length);
    List imag = new List(listData.length);
    for (var i = 0; i < listData.length; i++) {
    listData[i] = listData[i].cround2;
    real[i] = listData[i].real;
    imag[i] = listData[i].imag;
    }
    webSocket.send(json.stringify({"real": real, "imag": imag}));
    } else {
    webSocket.send(json.stringify({"real": listData, "imag": null}));
    }
    }
    },
    onDone: () {
    print('Connection closed by client: Status - ${webSocket.closeCode}'
    ' : Reason - ${webSocket.closeReason}');
    server.close();
    });
    });
    });
    }
    }

    关于dart - 将字符串列表写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13626709/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com