ZetCode

Dart FileMode

last modified October 18, 2026

The FileMode class in Dart defines constants for file opening modes. It's used with file operations to specify how a file should be opened.

FileMode is essential for controlling file access permissions and behavior. It's part of Dart's dart:io library for file system operations.

Basic Definition

FileMode is an enum-like class with constants for file operations. It determines whether to read, write, append, or create files.

Key modes include read, write, append, writeOnly, and writeOnlyAppend. These control file access and whether to create or truncate existing files.

Reading a File

This example shows how to open a file in read-only mode using FileMode.read.

main.dart
import 'dart:io';
import 'dart:convert';

void main() async {
  var file = File('example.txt');
  
  try {
    await file.writeAsString('Hello, Dart!\nThis is a test file.\n');
    
    var raf = await file.open(mode: FileMode.read);
    var length = await raf.length();
    var contents = await raf.read(length);
    print(utf8.decode(contents));
    
    await raf.close();
  } catch (e) {
    print('Error: $e');
  }
}

We write a small sample file, then open it in read mode and read its contents through a RandomAccessFile. FileMode.read ensures we only have read access to the file. The file must exist for this mode.

$ dart main.dart
Hello, Dart!
This is a test file.

Writing to a File

This example demonstrates writing to a file using FileMode.write.

main.dart
import 'dart:io';

void main() async {
  var file = File('output.txt');
  
  try {
    var sink = file.openWrite(mode: FileMode.write);
    sink.write('First line\n');
    sink.write('Second line\n');
    await sink.close();
    
    print('File written successfully');
  } catch (e) {
    print('Error: $e');
  }
}

FileMode.write opens the file for writing, creating it if needed. If the file exists, it's truncated to zero length first. We use openWrite for convenience.

$ dart main.dart
File written successfully

Appending to a File

This example shows how to append content to an existing file.

main.dart
import 'dart:io';

void main() async {
  var file = File('log.txt');
  
  try {
    var sink = file.openWrite(mode: FileMode.append);
    sink.write('${DateTime.now()}: New log entry\n');
    await sink.close();
    
    print('Log entry added');
  } catch (e) {
    print('Error: $e');
  }
}

FileMode.append opens the file for writing at the end. If the file doesn't exist, it's created. Existing content is preserved and new content is added at the end.

$ dart main.dart
Log entry added

Read-Write Access

This example demonstrates opening a file with both read and write access.

main.dart
import 'dart:io';
import 'dart:convert';

void main() async {
  var file = File('data.txt');
  
  try {
    await file.writeAsString('Initial content\n');
    
    var raf = await file.open(mode: FileMode.append);
    await raf.setPosition(0);
    var length = await raf.length();
    var content = await raf.read(length);
    await raf.setPosition(length);
    await raf.writeString('Additional content\n');
    await raf.close();
    
    print('Updated content:\n${utf8.decode(content)}');
  } catch (e) {
    print('Error: $e');
  }
}

FileMode.append opens the file for both reading and writing without truncating it. We seek back to position 0 to read the existing content, then seek to the end before appending the new line. Dart has no separate read-write mode that opens an existing file without truncating it; append is the closest match.

$ dart main.dart
Updated content:
Initial content

Write-Only Access

This example shows how to open a file for writing only.

main.dart
import 'dart:io';

void main() async {
  var file = File('newfile.txt');
  
  try {
    var raf = await file.open(mode: FileMode.writeOnly);
    await raf.writeString('This is a new file\n');
    await raf.close();
    
    print('File written in write-only mode');
  } on FileSystemException catch (e) {
    print('Error: ${e.message}');
  }
}

FileMode.writeOnly opens the file for writing only, creating it if needed and truncating it if it already exists. If you need exclusive creation, use File.create(exclusive: true), which fails when the path already exists; FileMode itself has no exclusive-create variant.

$ dart main.dart
File written in write-only mode

Best Practices

Source

Dart FileMode Documentation

This tutorial covered Dart's FileMode class with practical examples showing different file access patterns and their appropriate use cases.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.