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:ioworks on desktop, mobile and servers, but not in the browser or DartPad. Save the examples in this chapter to a file and run them withdart run.
Lessons in this chapter
- 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.
- Writing Files in DartLearn how to write files in Dart: create and overwrite with writeAsString, append, write bytes, and write large files with IOSink.
- 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.
- Working with Directories in DartLearn directory handling in Dart: create, list, search, copy and delete folders, and find the current and temporary directories.
- 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.
- File Attributes and Metadata in DartRead file attributes in Dart: size, last modified and accessed times, type and permissions, using length, lastModified and stat.
- Handling File Errors in DartHandle file system errors in Dart: FileSystemException, missing files, permission problems, and writing reliable file code.