ZetCode

Dart StdioType

最后修改于 2025 年 4 月 4 日

Dart 中的 StdioType 类提供了一种识别标准 IO 流类型的方法。它用于确定文件描述符是终端、管道、文件还是其他类型的流。

StdioType 是 Dart 的 dart:io 库的一部分,在处理进程通信或需要根据 IO 流类型调整行为时特别有用。

基本定义

StdioType 是一个枚举,用于标识不同的标准 IO 流类型。它有助于确定如何处理各种 IO 操作。

该类提供了 TERMINALPIPEFILEOTHER 等常量来对标准流进行分类。它通常与 stdinstdoutstderr 一起使用。

检查 stdin 类型

此示例显示了如何检查标准输入流的类型。

main.dart
import 'dart:io';

void main() {
  var stdinType = stdin.stdioType;
  
  if (stdinType == StdioType.TERMINAL) {
    print('Input is from a terminal');
  } else if (stdinType == StdioType.PIPE) {
    print('Input is from a pipe');
  } else {
    print('Input is of type: $stdinType');
  }
}

我们使用 stdioType 属性检查 stdin 的类型。这有助于确定程序是正在交互地接收输入还是通过管道接收输入。

$ dart main.dart
Input is from a terminal

$ echo "test" | dart main.dart
Input is from a pipe

检测输出流类型

此示例演示了如何检查标准输出流的类型。

main.dart
import 'dart:io';

void main() {
  checkStreamType(stdout, 'stdout');
  checkStreamType(stderr, 'stderr');
}

void checkStreamType(IOSink stream, String name) {
  var type = stream.stdioType;
  
  print('$name type: $type');
  print('Is terminal: ${type == StdioType.TERMINAL}');
}

我们检查 stdout 和 stderr 流以确定它们的类型。这对于根据目标以不同的方式格式化输出可能很有用。

$ dart main.dart
stdout type: StdioType.TERMINAL
Is terminal: true
stderr type: StdioType.TERMINAL
Is terminal: true

$ dart main.dart > output.txt
stdout type: StdioType.FILE
Is terminal: false
stderr type: StdioType.TERMINAL
Is terminal: true

处理不同的流类型

此示例展示了如何根据流类型调整行为。

main.dart
import 'dart:io';

void main() {
  var output = stdout;
  
  switch (output.stdioType) {
    case StdioType.TERMINAL:
      output.writeln('Writing to terminal - adding colors');
      break;
    case StdioType.PIPE:
      output.writeln('Writing to pipe - plain output');
      break;
    case StdioType.FILE:
      output.writeln('Writing to file - no terminal features');
      break;
    default:
      output.writeln('Writing to unknown stream type');
  }
}

我们使用 switch 语句来不同地处理不同的输出流类型。这种模式对于适应其环境的 CLI 工具很常见。

$ dart main.dart
Writing to terminal - adding colors

$ dart main.dart | cat
Writing to pipe - plain output

检查文件描述符

此示例演示了如何为任意文件描述符检查 StdioType。

main.dart
import 'dart:io';

void main() async {
  var file = File('test.txt');
  var sink = file.openWrite();
  
  print('File stream type: ${sink.stdioType}');
  
  await sink.close();
  
  var socket = await Socket.connect('google.com', 80);
  print('Socket stream type: ${socket.stdioType}');
  socket.destroy();
}

我们检查不同 IO 对象的 StdioType。请注意,并非所有 IO 对象都支持 stdioType,有些可能返回 null 或 OTHER。

$ dart main.dart
File stream type: StdioType.FILE
Socket stream type: StdioType.OTHER

使用 StdioType 进行进程通信

此示例展示了如何在进程通信场景中使用 StdioType。

main.dart
import 'dart:io';

void main() async {
  var process = await Process.start('ls', ['-l']);
  
  print('Process stdin type: ${process.stdin.stdioType}');
  print('Process stdout type: ${process.stdout.stdioType}');
  print('Process stderr type: ${process.stderr.stdioType}');
  
  await process.exitCode;
}

在生成进程时,StdioType 有助于确定如何处理进程流。这对于构建进程管理工具很有用。

$ dart main.dart
Process stdin type: StdioType.PIPE
Process stdout type: StdioType.PIPE
Process stderr type: StdioType.PIPE

最佳实践

来源

Dart StdioType 文档

本教程通过实际示例介绍了 Dart 的 StdioType 类,展示了如何在 Dart 中识别和处理不同的标准 IO 流类型。

作者

我叫 Jan Bodnar,我是一名热情的程序员,拥有丰富的编程经验。自 2007 年以来,我一直在撰写编程文章。迄今为止,我已撰写了 1,400 多篇文章和 8 本电子书。我在编程教学方面拥有十多年的经验。

列出 所有 Dart 教程