Dateisystem und Pfade je Plattform
Lesen und schreiben Sie portabel Dateien auf verschiedenen Targets.
Dateisystem und Pfade je Plattform ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Kotlin Multiplatform Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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(): StringAndroid 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.pathiOS 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 StringBuild 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. 🎉
Häufig gestellte Fragen
Ist die Lektion „Dateisystem und Pfade je Plattform“ kostenlos?
Ja — der vollständige Text von „Dateisystem und Pfade je Plattform“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Kotlin Multiplatform Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Dateisystem und Pfade je Plattform“?
Lesen und schreiben Sie portabel Dateien auf verschiedenen Targets. Du übst Kotlin Multiplatform Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Kotlin Multiplatform Academy zu starten?
Keine Vorkenntnisse erforderlich. Kotlin Multiplatform Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Dateisystem und Pfade je Plattform“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Kotlin Multiplatform Academy-Lektion Code schreiben und ausführen?
Ja. Jede Kotlin Multiplatform Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Eine Capability-Schnittstelle entwerfen
- expect/actual für Geräteinformationen
- Dateisystem und Pfade je Plattform
- Plattformimplementierungen injizieren