各平台的文件系统与路径
以可移植方式跨目标读取和写入文件
各平台的文件系统与路径 是 CoddyKit 上的免费 Kotlin Multiplatform Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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. 🎉
用 AI 导师学习 Kotlin — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 30
- 课程
- 120
常见问题解答
「各平台的文件系统与路径」课时是免费的吗?
是的 — 「各平台的文件系统与路径」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Kotlin Multiplatform Academy 课程的其余内容,请升级到 CoddyKit PRO。 Kotlin Multiplatform Academy 课程共包含 4 节课。
「各平台的文件系统与路径」这节课中我会学到什么?
以可移植方式跨目标读取和写入文件 你通过在浏览器中直接运行的动手代码来练习 Kotlin Multiplatform Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Kotlin Multiplatform Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Kotlin Multiplatform Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「各平台的文件系统与路径」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Kotlin Multiplatform Academy 课中编写并运行代码吗?
能。每节 Kotlin Multiplatform Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 设计能力接口
- 使用 expect/actual 获取设备信息
- 各平台的文件系统与路径
- 注入平台实现