0Pricing
Kotlin Multiplatform Academy · درس

نظام الملفات والمسارات لكل منصة

اقرأ الملفات واكتبها بطريقة قابلة للنقل عبر الأهداف المختلفة.

نظام الملفات والمسارات لكل منصة درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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(): 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. 🎉

الأسئلة الشائعة

هل درس «نظام الملفات والمسارات لكل منصة» مجاني؟

نعم — نص درس «نظام الملفات والمسارات لكل منصة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

ماذا ستتعلم في «نظام الملفات والمسارات لكل منصة»؟

اقرأ الملفات واكتبها بطريقة قابلة للنقل عبر الأهداف المختلفة. تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟

لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «نظام الملفات والمسارات لكل منصة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟

نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تصميم واجهة للقدرات
  2. expect/actual لمعلومات الجهاز
  3. نظام الملفات والمسارات لكل منصة
  4. حقن تطبيقات المنصة
← العودة إلى Kotlin Multiplatform Academy