File System Access

File System Access in Dart#

Introduction#

File system access in Dart allows you to interact with files and directories on the user’s device. Using the dart:io library, you can perform various operations such as reading, writing, and managing files and directories. Proper file system access is essential for applications that handle user data, configuration files, or any other file-based operations.

About#

The dart:io library provides a range of classes and methods for file system access:

  • File: For handling file operations such as reading, writing, and deleting files.
  • Directory: For managing directories, including creating, listing, and deleting directories.
  • FileSystemEntity: A base class for files and directories, allowing common operations.
  • RandomAccessFile: For reading and writing files using random access.

These classes allow for synchronous and asynchronous file operations, providing flexibility based on application needs.

How to Use#

Key Classes and Methods#

  • File Class:

    • readAsString(): Reads the entire file as a string.
    • writeAsString(): Writes a string to a file.
    • delete(): Deletes a file.
  • Directory Class:

    • create(): Creates a new directory.
    • list(): Lists the contents of the directory.
    • delete(): Deletes a directory.
  • FileSystemEntity Class:

    • exists(): Checks if a file or directory exists.

Example Dependencies#

dependencies:
  path: ^1.8.0
  • Import Statement
import 'dart:io';
import 'package:path/path.dart' as p; 

How It Works#

  1. File Operations:
  • Use the File class for reading, writing, and deleting files. Methods like readAsString() and writeAsString() allow you to handle file content easily. 2 Directory Operations:

  • Use the Directory class to manage directories. Methods such as create() and list() help in creating directories and listing their contents.

  1. FileSystemEntity Operations:
  • The FileSystemEntity class provides basic operations to check for existence and perform common tasks with files and directories.

Example#

  • Example 1: Reading a File
import 'dart:io';

void main() async {
  File file = File('example.txt');
  
  try {
    String content = await file.readAsString();
    print('File content: $content');
  } catch (e) {
    print('Error reading file: $e');
  }
}
  • Example 2: Writing to a File
import 'dart:io';

void main() async {
  File file = File('example.txt');
  
  try {
    await file.writeAsString('Hello, Dart!');
    print('File written successfully.');
  } catch (e) {
    print('Error writing to file: $e');
  }
}
  • Example 3: Creating and Listing a Directory
import 'dart:io';

void main() async {
  Directory dir = Directory('example_dir');
  
  try {
    // Create a directory
    await dir.create();
    print('Directory created.');

    // List contents
    await for (var entity in dir.list()) {
      print('Found ${entity.path}');
    }
  } catch (e) {
    print('Error with directory: $e');
  }
}
  • Example 4: Deleting a File
import 'dart:io';

void main() async {
  File file = File('example.txt');
  
  try {
    await file.delete();
    print('File deleted.');
  } catch (e) {
    print('Error deleting file: $e');
  }
}

Summary#

  • Dart’s dart:io library provides powerful tools for file and directory manipulation.
  • Use the File class for operations on files, Directory for managing directories, and FileSystemEntity for common tasks.
  • Asynchronous methods help ensure non-blocking file operations, improving application performance.

This format provides a concise yet comprehensive overview of file system access in Dart, with clear examples and explanations.