プラットフォームごとのファイルシステムとパス
ターゲット間で移植可能な形でファイルを読み書きします
「プラットフォームごとのファイルシステムとパス」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🎉
よくある質問
「プラットフォームごとのファイルシステムとパス」レッスンは無料ですか?
はい。「プラットフォームごとのファイルシステムとパス」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「プラットフォームごとのファイルシステムとパス」で何を学びますか?
ターゲット間で移植可能な形でファイルを読み書きします ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「プラットフォームごとのファイルシステムとパス」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 機能インターフェースを設計する
- デバイス情報のexpect/actual
- プラットフォームごとのファイルシステムとパス
- プラットフォーム実装を注入する