File, socket, HTTP, and I/O support for non-web applications
The dart:io library provides file, socket, HTTP, and other I/O support for non-web applications. This library is only available for Dart Native platforms (servers, command-line scripts, Flutter mobile and desktop apps).
Important: Browser-based apps cannot use this library. The dart:io library is only available on:
A reference to a directory on the file system. Provides methods for creating, listing, and managing directories.
Directory Operations
// Creating a directory referencevar dir = Directory('my_directory');// Create directoryawait dir.create();// Create directory with parent directoriesawait dir.create(recursive: true);// Check if directory existsif (await dir.exists()) { print('Directory exists');}// List directory contentsawait for (var entity in dir.list()) { if (entity is File) { print('File: ${entity.path}'); } else if (entity is Directory) { print('Directory: ${entity.path}'); }}// List recursivelyawait for (var entity in dir.list(recursive: true)) { print(entity.path);}// List only filesvar files = dir.list() .where((entity) => entity is File) .map((entity) => entity as File);// Delete directory (must be empty)await dir.delete();// Delete directory and contentsawait dir.delete(recursive: true);// Rename directoryvar renamed = await dir.rename('new_name');// Get current directoryvar current = Directory.current;print('Current directory: ${current.path}');// Change current directoryDirectory.current = '/path/to/new/directory';// Get system temp directoryvar temp = Directory.systemTemp;var tempDir = await temp.createTemp('myapp_');
Base class for File, Directory, and Link. Provides common file system operations.
// Check type of pathvar path = 'some/path';if (await FileSystemEntity.isFile(path)) { print('Path is a file');} else if (await FileSystemEntity.isDirectory(path)) { print('Path is a directory');} else if (await FileSystemEntity.isLink(path)) { print('Path is a link');}// Get entity typevar type = await FileSystemEntity.type(path);if (type == FileSystemEntityType.file) { var file = File(path);}// Check if path existsif (await FileSystemEntity.type(path) != FileSystemEntityType.notFound) { print('Path exists');}// Get parent directoryvar file = File('/path/to/file.txt');var parent = file.parent; // Directory('/path/to')
import 'dart:io';import 'dart:convert';// Write to stdoutstdout.write('Enter your name: ');// Read from stdinvar name = stdin.readLineSync();print('Hello, $name!');// Read as streamstdout.write('Enter lines (Ctrl+D to end):\n');await stdin .transform(utf8.decoder) .transform(LineSplitter()) .forEach((line) => print('You entered: $line'));// Write to stderrstderr.writeln('This is an error message');// Exit programexit(0); // Successexit(1); // Error