انتقل إلى المحتوى
أمين أبوزيد
BEG-034

الملفات والإدخال/الإخراج (I/O)

  • 6 درس
  • 67 دقيقة قراءة
  • 3 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • المسارات (Paths)
  • Files API
  • قراءة/كتابة النصوص
  • التدفقات (streams)
  • الإدخال/الإخراج المؤقت (buffered I/O)

تعلّم أولًا

67 دقيقة قراءة · 3 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Build and resolve paths with Path and check existence with Files.
  2. Read and write text files with Files.readAllLines, Files.writeString, and buffered readers/writers.
  3. Explain the difference between byte streams and character streams and when encoding matters.
  4. Handle I/O errors and always close resources.

الدروس

  1. 01المسارات وFiles API11 د قراءة
  2. 02قراءة النصوص وكتابتها10 د قراءة
  3. 03التدفقات والقارئات والتخزين المؤقت11 د قراءة
  4. 04ترميز الأحرف (UTF-8)12 د قراءة
  5. 05صيغة CSV بسيطة11 د قراءة
  6. 06عرض عملي: حفظ جهات الاتصال وتحميلها بصيغة CSV12 د قراءة

المختبر والتمارين

Lab

BEG-034-LAB1 · Contacts persistence

Outcomes: 1–4 · Time: ~2 h Save contacts to CSV on exit and load them on start. Survive a missing or corrupt file. Use UTF-8 and relative paths. Acceptance: data survives restarts; bad lines reported, not fatal.

Exercises (assessment: three exercises)

IDOutcomeTask
BEG-034-EX011, 2Create a data folder if missing, write three lines to data/notes.txt, read them back and print them with line numbers.
BEG-034-EX022, 4Write a method that copies a text file line by line with BufferedReader and BufferedWriter in try-with-resources, printing a clear message on IOException.
BEG-034-EX033Explain when to use a byte stream vs a character stream, and what goes wrong if the encoding differs between writing and reading.
أظهر الحلول
  • EX01 Files.createDirectories(Path.of("data")); Files.write(p, List.of("a","b","c")); then List<String> ls = Files.readAllLines(p); for (int i=0;i<ls.size();i++) System.out.println((i+1)+": "+ls.get(i));
  • EX02 try (var in = Files.newBufferedReader(src); var out = Files.newBufferedWriter(dst)) { String l; while ((l = in.readLine()) != null) { out.write(l); out.newLine(); } } catch (IOException e) { System.out.println("Copy failed: " + e.getMessage()); }
  • EX03 Byte streams for binary data (images, archives); character streams for text. Different encodings produce garbled characters (for example Arabic letters turn into symbols), so always use UTF-8 explicitly.
  • Outcome 4 (always close) is also checked by the lab and quiz Q09.

الاختبار

Outcomes: O1 Path and Files · O2 reading and writing text · O3 byte vs character streams, encoding · O4 errors and closing.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Creating a Path object:O1
  2. 2Check whether a file exists:O1
  3. 3Which path style travels well between computers?O1
  4. 4Files.readAllLines(path) returns:O2
  5. 5Files.writeString(path, text) by default:O2
  6. 6BufferedReader is useful because it:O2
  7. 7Raw binary files such as images are best handled with:O3
  8. 8The recommended text encoding is:O3
  9. 9The safest way to ensure a file is closed:O4
  10. 10IOException is:O4

الجزء العملي

Three exercises BEG-034-EX01 to EX03 plus the lab. Pass: all three exercises correct and the lab survives a missing file and a corrupt line.

أخطاء شائعة

Hard-coded absolute paths; platform-default encoding; unclosed files.

التقييم

3 exercises.

الصلة بالمسار الوظيفي

File uploads (JNR-023) and logs (JNR-006).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.