FileSystem

expect abstract class FileSystem

Read and write access to a hierarchical collection of files, addressed by paths. This is a natural interface to the current computer's local file system.

Other implementations are possible:

  • FakeFileSystem is an in-memory file system suitable for testing. Note that this class is included in the okio-fakefilesystem artifact.

  • ForwardingFileSystem is a file system decorator. Use it to apply monitoring, encryption, compression, or filtering to another file system.

  • A ZIP file system could provide access to the contents of a .zip file.

For improved capability and testability, consider structuring your classes to dependency inject a FileSystem rather than using FileSystem.SYSTEM directly.

Small API

---------

This interface is deliberately limited in which features it supports.

It is not suitable for high-latency or unreliable remote file systems. It lacks support for retries, timeouts, cancellation, and bulk operations.

It cannot create special file types like hard links, pipes, or mounts. Reading or writing these files works as if they were regular files.

It cannot read or write file access control features like the UNIX chmod and Windows access control lists. It does honor these controls and will fail with an IOException if privileges are insufficient!

It cannot lock files or check which files are locked.

It cannot watch the file system for changes.

Applications that need rich file system features should use another API!

Multiplatform

-------------

This class supports a matrix of Kotlin platforms (JVM, Kotlin/Native, Kotlin/JS) and operating systems (Linux, macOS, and Windows). It attempts to balance working similarly across platforms with being consistent with the local operating system.

This is a blocking API which limits its applicability on concurrent Node.js services. File operations will block the event loop (and all JavaScript execution!) until they complete.

It supports the path schemes of both Windows (like C:\Users) and UNIX (like /home). Note that path resolution rules differ by platform.

Differences vs. Java IO APIs

----------------------------

The java.io.File class is Java's original file system API. The delete and renameTo methods return false if the operation failed. The list method returns null if the file isn't a directory or could not be listed. This class always throws an IOException when an operation doesn't succeed.

The java.nio.file.Path and java.nio.file.Files classes are the entry points of Java's new file system API. Each Path instance is scoped to a particular file system, though that is often implicit because the Paths.get() function automatically uses the default (ie. system) file system. In Okio's API paths are just identifiers; you must use a specific FileSystem object to do I/O with.

Inheritors

actual abstract class FileSystem
actual abstract class FileSystem
actual abstract class FileSystem
actual abstract class FileSystem

Constructors

Link copied to clipboard
expect constructor()
constructor()
constructor()
constructor()
constructor()

Types

Link copied to clipboard
expect object Companion
actual object Companion
actual object Companion
actual object Companion
actual object Companion

Functions

Link copied to clipboard
expect abstract fun appendingSink(file: Path, mustExist: Boolean = false): Sink

Returns a sink that appends bytes to the end of file, creating it if it doesn't already exist.

actual abstract fun appendingSink(file: Path, mustExist: Boolean): Sink
fun appendingSink(file: Path): Sink
actual abstract fun appendingSink(file: Path, mustExist: Boolean): Sink
actual abstract fun appendingSink(file: Path, mustExist: Boolean): Sink
actual abstract fun appendingSink(file: Path, mustExist: Boolean): Sink
Link copied to clipboard
expect abstract fun atomicMove(source: Path, target: Path)

Moves source to target in-place if the underlying file system supports it. If target exists, it is first removed. If source == target, this operation does nothing. This may be used to move a file or a directory.

actual abstract fun atomicMove(source: Path, target: Path)
actual abstract fun atomicMove(source: Path, target: Path)
actual abstract fun atomicMove(source: Path, target: Path)
actual abstract fun atomicMove(source: Path, target: Path)
Link copied to clipboard
expect abstract fun canonicalize(path: Path): Path

Resolves path against the current working directory and symlinks in this file system. The returned path identifies the same file as path, but with an absolute path that does not include any symbolic links.

actual abstract fun canonicalize(path: Path): Path
actual abstract fun canonicalize(path: Path): Path
actual abstract fun canonicalize(path: Path): Path
actual abstract fun canonicalize(path: Path): Path
Link copied to clipboard
expect open fun copy(source: Path, target: Path)

Copies all the bytes from the file at source to the file at target. This does not copy file metadata like last modified time, permissions, or extended attributes.

actual open fun copy(source: Path, target: Path)
actual open fun copy(source: Path, target: Path)
actual open fun copy(source: Path, target: Path)
actual open fun copy(source: Path, target: Path)
Link copied to clipboard
expect fun createDirectories(dir: Path, mustCreate: Boolean = false)

Creates a directory at the path identified by dir, and any enclosing parent path directories, recursively.

actual fun createDirectories(dir: Path, mustCreate: Boolean)
actual fun createDirectories(dir: Path, mustCreate: Boolean)
actual fun createDirectories(dir: Path, mustCreate: Boolean)
actual fun createDirectories(dir: Path, mustCreate: Boolean)
Link copied to clipboard
expect abstract fun createDirectory(dir: Path, mustCreate: Boolean = false)

Creates a directory at the path identified by dir.

actual abstract fun createDirectory(dir: Path, mustCreate: Boolean)
actual abstract fun createDirectory(dir: Path, mustCreate: Boolean)
actual abstract fun createDirectory(dir: Path, mustCreate: Boolean)
actual abstract fun createDirectory(dir: Path, mustCreate: Boolean)
Link copied to clipboard
expect abstract fun createSymlink(source: Path, target: Path)

Creates a symbolic link at source that resolves to target. If target is a relative path, it is relative to source.parent.

actual abstract fun createSymlink(source: Path, target: Path)
actual abstract fun createSymlink(source: Path, target: Path)
actual abstract fun createSymlink(source: Path, target: Path)
actual abstract fun createSymlink(source: Path, target: Path)
Link copied to clipboard
expect abstract fun delete(path: Path, mustExist: Boolean = false)

Deletes the file or directory at path.

actual abstract fun delete(path: Path, mustExist: Boolean)
fun delete(path: Path)
actual abstract fun delete(path: Path, mustExist: Boolean)
actual abstract fun delete(path: Path, mustExist: Boolean)
actual abstract fun delete(path: Path, mustExist: Boolean)
Link copied to clipboard
expect open fun deleteRecursively(fileOrDirectory: Path, mustExist: Boolean = false)

Recursively deletes all children of fileOrDirectory if it is a directory, then deletes fileOrDirectory itself.

actual open fun deleteRecursively(fileOrDirectory: Path, mustExist: Boolean)
fun deleteRecursively(fileOrDirectory: Path)
actual open fun deleteRecursively(fileOrDirectory: Path, mustExist: Boolean)
actual open fun deleteRecursively(fileOrDirectory: Path, mustExist: Boolean)
actual open fun deleteRecursively(fileOrDirectory: Path, mustExist: Boolean)
Link copied to clipboard
expect fun exists(path: Path): Boolean

Returns true if path identifies an object on this file system.

actual fun exists(path: Path): Boolean
actual fun exists(path: Path): Boolean
actual fun exists(path: Path): Boolean
actual fun exists(path: Path): Boolean
Link copied to clipboard
expect abstract fun list(dir: Path): List<Path>

Returns the children of dir. The returned list is sorted using natural ordering. If dir is a relative path, the returned elements will also be relative paths. If it is an absolute path, the returned elements will also be absolute paths.

actual abstract fun list(dir: Path): List<Path>
actual abstract fun list(dir: Path): List<Path>
actual abstract fun list(dir: Path): List<Path>
actual abstract fun list(dir: Path): List<Path>
Link copied to clipboard
expect abstract fun listOrNull(dir: Path): List<Path>?

Returns the children of the directory identified by dir. The returned list is sorted using natural ordering. If dir is a relative path, the returned elements will also be relative paths. If it is an absolute path, the returned elements will also be absolute paths.

actual abstract fun listOrNull(dir: Path): List<Path>?
actual abstract fun listOrNull(dir: Path): List<Path>?
actual abstract fun listOrNull(dir: Path): List<Path>?
actual abstract fun listOrNull(dir: Path): List<Path>?
Link copied to clipboard
expect open fun listRecursively(dir: Path, followSymlinks: Boolean = false): Sequence<Path>

Returns a sequence that lazily traverses the children of dir using repeated calls to list. If none of dir's children are directories this returns the same elements as list.

actual open fun listRecursively(dir: Path, followSymlinks: Boolean): Sequence<Path>
actual open fun listRecursively(dir: Path, followSymlinks: Boolean): Sequence<Path>
actual open fun listRecursively(dir: Path, followSymlinks: Boolean): Sequence<Path>
actual open fun listRecursively(dir: Path, followSymlinks: Boolean): Sequence<Path>
Link copied to clipboard
expect fun metadata(path: Path): FileMetadata

Returns metadata of the file, directory, or object identified by path.

actual fun metadata(path: Path): FileMetadata
actual fun metadata(path: Path): FileMetadata
actual fun metadata(path: Path): FileMetadata
actual fun metadata(path: Path): FileMetadata
Link copied to clipboard
expect abstract fun metadataOrNull(path: Path): FileMetadata?

Returns metadata of the file, directory, or object identified by path. This returns null if there is no file at path.

actual abstract fun metadataOrNull(path: Path): FileMetadata?
actual abstract fun metadataOrNull(path: Path): FileMetadata?
actual abstract fun metadataOrNull(path: Path): FileMetadata?
actual abstract fun metadataOrNull(path: Path): FileMetadata?
Link copied to clipboard
expect abstract fun openReadOnly(file: Path): FileHandle

Returns a handle to read file. This will fail if the file doesn't already exist.

actual abstract fun openReadOnly(file: Path): FileHandle
actual abstract fun openReadOnly(file: Path): FileHandle
actual abstract fun openReadOnly(file: Path): FileHandle
actual abstract fun openReadOnly(file: Path): FileHandle
Link copied to clipboard
expect abstract fun openReadWrite(file: Path, mustCreate: Boolean = false, mustExist: Boolean = false): FileHandle

Returns a handle to read and write file. This will create the file if it doesn't already exist.

actual abstract fun openReadWrite(file: Path, mustCreate: Boolean, mustExist: Boolean): FileHandle
fun openReadWrite(file: Path): FileHandle
actual abstract fun openReadWrite(file: Path, mustCreate: Boolean, mustExist: Boolean): FileHandle
actual abstract fun openReadWrite(file: Path, mustCreate: Boolean, mustExist: Boolean): FileHandle
actual abstract fun openReadWrite(file: Path, mustCreate: Boolean, mustExist: Boolean): FileHandle
Link copied to clipboard
fun FileSystem.openZip(zipPath: Path): FileSystem
Link copied to clipboard
expect inline fun <T> read(file: Path, readerAction: BufferedSource.() -> T): T

Creates a source to read file, executes readerAction to read it, and then closes the source. This is a compact way to read the contents of a file.

actual inline fun <T> read(file: Path, readerAction: BufferedSource.() -> T): T
@JvmName(name = "-read")
actual inline fun <T> read(file: Path, readerAction: BufferedSource.() -> T): T
actual inline fun <T> read(file: Path, readerAction: BufferedSource.() -> T): T
actual inline fun <T> read(file: Path, readerAction: BufferedSource.() -> T): T
Link copied to clipboard
expect abstract fun sink(file: Path, mustCreate: Boolean = false): Sink

Returns a sink that writes bytes to file from beginning to end. If file already exists it will be replaced with the new data.

actual abstract fun sink(file: Path, mustCreate: Boolean): Sink
fun sink(file: Path): Sink
actual abstract fun sink(file: Path, mustCreate: Boolean): Sink
actual abstract fun sink(file: Path, mustCreate: Boolean): Sink
actual abstract fun sink(file: Path, mustCreate: Boolean): Sink
Link copied to clipboard
expect abstract fun source(file: Path): Source

Returns a source that reads the bytes of file from beginning to end.

actual abstract fun source(file: Path): Source
actual abstract fun source(file: Path): Source
actual abstract fun source(file: Path): Source
actual abstract fun source(file: Path): Source
Link copied to clipboard
expect inline fun <T> write(file: Path, mustCreate: Boolean = false, writerAction: BufferedSink.() -> T): T

Creates a sink to write file, executes writerAction to write it, and then closes the sink. This is a compact way to write a file.

actual inline fun <T> write(file: Path, mustCreate: Boolean, writerAction: BufferedSink.() -> T): T
@JvmName(name = "-write")
actual inline fun <T> write(file: Path, mustCreate: Boolean, writerAction: BufferedSink.() -> T): T
actual inline fun <T> write(file: Path, mustCreate: Boolean, writerAction: BufferedSink.() -> T): T
actual inline fun <T> write(file: Path, mustCreate: Boolean, writerAction: BufferedSink.() -> T): T