Dart Tutorial

Dart Chapter 11 of 15

Files and Directories

Read, write, check and delete files, work with folders and paths, and handle file system errors using the dart:io library.

Command-line tools, servers and desktop apps all need to read and write files. Dart’s dart:io library provides everything required.

dart:io works on desktop, mobile and servers, but not in the browser or DartPad. Save the examples in this chapter to a file and run them with dart run.

Lessons in this chapter

  1. Reading Files in DartLearn how to read files in Dart with dart:io: whole file as text, line by line, as bytes, as a stream, and sync versus async methods.
  2. Writing Files in DartLearn how to write files in Dart: create and overwrite with writeAsString, append, write bytes, and write large files with IOSink.
  3. Checking If a File Exists and Deleting Files in DartLearn to check whether a file or directory exists in Dart, and to delete, rename, move and copy files safely with dart:io.
  4. Working with Directories in DartLearn directory handling in Dart: create, list, search, copy and delete folders, and find the current and temporary directories.
  5. File Paths in Dart with the path PackageLearn to build and manipulate file paths in Dart safely across Windows, macOS and Linux using the path package: join, basename and more.
  6. File Attributes and Metadata in DartRead file attributes in Dart: size, last modified and accessed times, type and permissions, using length, lastModified and stat.
  7. Handling File Errors in DartHandle file system errors in Dart: FileSystemException, missing files, permission problems, and writing reliable file code.