ระบบไฟล์และเส้นทางแยกตามแพลตฟอร์ม
อ่านและเขียนไฟล์ข้ามเป้าหมายได้อย่างพกพา
ระบบไฟล์และเส้นทางแยกตามแพลตฟอร์ม เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ระบบไฟล์และเส้นทางแยกตามแพลตฟอร์ม”
อ่านและเขียนไฟล์ข้ามเป้าหมายได้อย่างพกพา คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ระบบไฟล์และเส้นทางแยกตามแพลตฟอร์ม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ออกแบบอินเทอร์เฟซความสามารถ
- expect/actual สำหรับข้อมูลอุปกรณ์
- ระบบไฟล์และเส้นทางแยกตามแพลตฟอร์ม
- ฉีดการใช้งานของแพลตฟอร์ม