.transform(utf8.decoder) // Decode bytes to UTF-8.
.transform(LineSplitter()); // Convert stream to individual lines.
try {
awaitfor (var line in lines) {
print('$line: ${line.length} characters');
}
print('File is now closed.');
} catch (e) {
print('Error: $e');
}
// 第二种读取全部
intListStream.toList().then((value){
print(String.fromCharCodes(value[0]));
});
// 仍然可用
print(file.readAsStringSync());
}
/// 文件写入
// 写法一
import"dart:io";
Future<void> main() async {
File file = File("static/test.txt");
IOSink sink = file.openWrite();
sink.write("""
1,2
3,4
""");
sink.flush();
Future.microtask(() async {
await sink.done;
sink.close();
});
}
//官网做法
void main() {
var file = File('file.txt');
var sink = file.openWrite();
sink.write('FILE ACCESSED ${DateTime.now()}\n');
// Close the IOSink to free system resources.
sink.close();
}
想要将数据迅速写入文件中,可以将文件写入的过程放在一个Future.microtask()里面
sink.flush();
sink.close();连着使用报错 This seems to be working as intended. Calling close while an addStream is still active will give this result, not close the addStream.I've been wanting to change that, allowing you to use add, addError and close during an addStream, but it has not been a high priority. https://github.com/dart-lang/sdk/issues/41707
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步