tokio::fs module provides asynchronous file system utilities for reading and writing files without blocking the async runtime.
Overview
Tokio’s file system operations run on a blocking thread pool usingspawn_blocking behind the scenes, since most operating systems don’t provide true async file system APIs. This allows file operations to avoid blocking the async runtime’s executor threads.
Quick Start
The simplest way to work with files is using the utility functions:File Operations
Reading Files
Reads the entire contents of a file into a bytes vector.Parameters:
path: impl AsRef<Path>- Path to the file
io::Result<Vec<u8>>Reads the entire contents of a file into a string.Parameters:
path: impl AsRef<Path>- Path to the file
io::Result<String>Writing Files
Writes bytes to a file, creating or truncating it.Parameters:
path: impl AsRef<Path>- Path to the filecontents: impl AsRef<[u8]>- Contents to write
io::Result<()>Overwrites the file if it exists, creates it if it doesn’t.File Type
For more control, use theFile type:
Opens a file in read-only mode.Parameters:
path: impl AsRef<Path>- Path to the file
io::Result<File>Creates a new file or truncates an existing one.Parameters:
path: impl AsRef<Path>- Path to the file
io::Result<File>Returns a new OpenOptions object for fine-grained control.Returns:
OpenOptionsWriting to Files
Directory Operations
Creating Directories
Creates a new directory.Parameters:
path: impl AsRef<Path>- Path to create
io::Result<()>Returns an error if the directory already exists.Recursively creates a directory and all parent directories.Parameters:
path: impl AsRef<Path>- Path to create
io::Result<()>Reading Directories
Returns a stream over entries in a directory.Parameters:
path: impl AsRef<Path>- Directory path
io::Result<ReadDir>Removing Directories
Removes an empty directory.Parameters:
path: impl AsRef<Path>- Directory to remove
io::Result<()>Removes a directory and all its contents recursively.Parameters:
path: impl AsRef<Path>- Directory to remove
io::Result<()>File Metadata
Returns metadata about a file or directory.Parameters:
path: impl AsRef<Path>- File path
io::Result<Metadata>Returns metadata without following symbolic links.Parameters:
path: impl AsRef<Path>- File path
io::Result<Metadata>File System Operations
Renames or moves a file or directory.Parameters:
from: impl AsRef<Path>- Source pathto: impl AsRef<Path>- Destination path
io::Result<()>Copies a file to a new location.Parameters:
from: impl AsRef<Path>- Source fileto: impl AsRef<Path>- Destination path
io::Result<u64> - Number of bytes copiedRemoves a file from the filesystem.Parameters:
path: impl AsRef<Path>- File to remove
io::Result<()>Creates a hard link to a file.Parameters:
src: impl AsRef<Path>- Existing filedst: impl AsRef<Path>- New link path
io::Result<()>Returns the canonical, absolute form of a path.Parameters:
path: impl AsRef<Path>- Path to canonicalize
io::Result<PathBuf>Checks whether a path exists.Parameters:
path: impl AsRef<Path>- Path to check
io::Result<bool>Symbolic Links
Reads the target of a symbolic link.Parameters:
path: impl AsRef<Path>- Symlink path
io::Result<PathBuf>Platform-Specific
Unix:symlink(src, dst)- Creates a symbolic link
symlink_file(src, dst)- Creates a file symlinksymlink_dir(src, dst)- Creates a directory symlink
Permissions
Changes the permissions of a file or directory.Parameters:
path: impl AsRef<Path>- File pathperm: Permissions- New permissions
io::Result<()>Performance Tuning
Tokio’s file operations usespawn_blocking, which has performance implications. Batch operations for better performance:
Good: Single spawn_blocking call
Better: Use BufWriter for multiple writes
Best: Manual spawn_blocking for complex operations
Buffered I/O
UseBufReader and BufWriter to reduce the number of spawn_blocking calls:
OpenOptions
For fine-grained control over file opening:read(bool)- Open for readingwrite(bool)- Open for writingappend(bool)- Append to filetruncate(bool)- Truncate file to 0 bytescreate(bool)- Create if doesn’t existcreate_new(bool)- Create only if doesn’t exist
Examples
Counting lines without loading entire file
See Also
- AsyncRead - Async reading trait
- AsyncWrite - Async writing trait
- AsyncSeek - Async seeking trait
- BufReader - Buffered reader
- BufWriter - Buffered writer