How to send print() output directly to a file with

2019-07-17 23:57发布

问题:

I read those ch04-tools-editor, get-started and searched on Google, but I didn't find any answers. How to send print() output directly to a file with Dart Editor?

EDIT : I want to send (pipe/redirect stream) the data (what print() return) directly in a file, instead the Dart Editor. I'm looking for a feature of the Dart Editor.

回答1:

print() doesn't output to files; it outputs to the console (stdout in console apps, the browser console in browsers).

The dart:io library offers plenty of functionality for I/O, including reading and writing files. One example:

import 'dart:io';

void main() {
  var out = new File('output.txt').openWrite();
  out.write("String written to file.\n");
  out.close();
}

Update: As per your updated question, you're looking for a Dart Editor feature that automatically writes console output to a file. To the best of my knowledge, there's no such feature. Your options include:

  1. Manually write to a file as demonstrated above.
  2. Copy the console output from Dart Editor.
  3. Run your app from outside Dart Editor. On any UNIX-like system, for example, you can redirect stdout like this:

    dart my-app.dart >output.txt
    
  4. Modify Dart Editor (it's open source) to add the feature you want.