0Pricing
Kotlin Multiplatform Academy · Lesson

File System & Paths per Platform

Read and write files portably across targets.

File System & Paths per Platform is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Files Live in Different Places

Android and iOS store app files in totally different folders. Shared code needs a portable way to find a safe place to write. 📁

Declare a Paths expect

Put a small expect function in commonMain that returns the app's data directory as a plain String path.

expect fun dataDir(): String

Android Uses filesDir

The Android actual returns context.filesDir.path, the private folder the system guarantees for your app's files.

actual fun dataDir(): String =
  appContext.filesDir.path

iOS Uses the Documents Dir

The iOS actual asks NSFileManager for the Documents directory, the standard place an app keeps user data.

actual fun dataDir(): String =
  NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, true
  ).first() as String

Build Paths, Do Not Hardcode

Join the directory with a file name instead of writing a literal path. Hardcoded paths like /sdcard break the moment you switch platform.

val file = dataDir() + "/profile.json"

A Tiny Read/Write API

Wrap reading and writing behind a shared interface too, so callers save text without knowing the platform underneath.

interface Storage {
  fun write(name: String, text: String)
  fun read(name: String): String?
}

Write From Shared Code

Common logic calls storage.write knowing nothing about files or folders. The platform actual handles the real bytes on disk.

storage.write("profile.json", json)

Read It Back Safely

Return a nullable String on read so missing files do not crash. A null simply means the file is not there yet.

val saved = storage.read("profile.json")

Keep Paths Out of the Domain

Your business rules should never see a raw path. Hide every directory detail behind the capability so the domain stays clean.

Mind Permissions and Sandboxes

Both platforms sandbox app storage. Stick to the directories the system gives you and you avoid permission errors entirely.

Use okio for Real File Work

For serious reads and writes, a multiplatform library like okio gives one FileSystem API across targets, so you skip raw platform calls.

Quick Check

Think about where a portable directory path comes from.

Recap

You exposed a portable dataDir via expect/actual, built paths instead of hardcoding them, and hid file work behind a small storage capability. 🎉

Frequently asked questions

Is the “File System & Paths per Platform” lesson free?

Yes — the full text of “File System & Paths per Platform” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “File System & Paths per Platform”?

Read and write files portably across targets. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “File System & Paths per Platform” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Design a Capability Interface
  2. expect/actual for Device Info
  3. File System & Paths per Platform
  4. Inject Platform Implementations
← Back to Kotlin Multiplatform Academy