PlatformFile is the core class in FileKit that provides a unified representation of files across all platforms. It abstracts away platform-specific file implementations and provides a consistent API for working with files in your Kotlin Multiplatform project.
You can create a PlatformFile instance in several ways:
Copy
// From a path stringval file = PlatformFile("/path/to/file.txt")// From a kotlinx.io.files.Pathval path = Path("/path/to/file.txt")val file = PlatformFile(path)// From a parent file and child pathval parentDir = PlatformFile("/path/to")val file = PlatformFile(parentDir, "file.txt")// or using the convenient / operatorval file = parentDir / "file.txt"
Each platform also provides specific constructors:
Copy
// From a Java Fileval javaFile = File("/path/to/file.txt")val file = PlatformFile(javaFile)// From an Android Urival uri = Uri.parse("content://...")val file = PlatformFile(uri)
PlatformFile provides several properties to access file information:
Copy
val file = PlatformFile("/path/to/document.pdf")// Get the file name with extensionval name: String = file.name // "document.pdf"// Get just the file extensionval extension: String = file.extension // "pdf"// Get the file name without extensionval nameWithoutExtension: String = file.nameWithoutExtension // "document"// Get the file pathval path: String = file.path // "/path/to/document.pdf"// Get the file size in bytesval size: Long = file.size()// Get the parent directoryval parent: PlatformFile? = file.parent()// Get the absolute path stringval absolutePath: String = file.absolutePath()// Get the absolute fileval absoluteFile: PlatformFile = file.absoluteFile()
PlatformFile provides methods for common file operations:
Copy
// Check if the file existsval exists: Boolean = file.exists()// Check if it's a regular fileval isFile: Boolean = file.isRegularFile()// Check if it's a directoryval isDirectory: Boolean = file.isDirectory()// Check if it's an absolute pathval isAbsolute: Boolean = file.isAbsolute()// Create directoriesfile.createDirectories()// Copy the file to a new locationfile.copyTo(newFile)// Move the file to a new locationfile.atomicMove(newFile)// Delete the filefile.delete()// List files in the directoryval files: List<PlatformFile> = file.list()// Get input stream as a sourceval source: RawSource = file.source()// Get output stream as a sinkval sink: RawSink = file.sink(append = false)// Read bytes from the fileval bytes: ByteArray = file.readBytes()// Read string from the fileval content: String = file.readString()// Write bytes to the filefile.write(bytes)// Write string to the filefile.writeString(content)
You can use the resolve method or the / operator to navigate through directories:
Copy
val baseDir = PlatformFile("/path/to/base")// Create a reference to a subdirectory or fileval subDir = baseDir / "subdirectory"val file = subDir / "file.txt"// Alternative using resolveval file2 = baseDir.resolve("subdirectory/file.txt")// Get the absolute path from a relative pathval relativeDir = PlatformFile("/path/to/relative")val absoluteDir: PlatformFile = relativeDir.absoluteFile()