الملفات والإدخال/الإخراج (I/O)
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- المسارات (Paths)
- Files API
- قراءة/كتابة النصوص
- التدفقات (streams)
- الإدخال/الإخراج المؤقت (buffered I/O)
تعلّم أولًا
67 دقيقة قراءة · 3 ساعة تدريب (تقدير)
مخرجات التعلم
- Build and resolve paths with
Pathand check existence withFiles. - Read and write text files with
Files.readAllLines,Files.writeString, and buffered readers/writers. - Explain the difference between byte streams and character streams and when encoding matters.
- Handle I/O errors and always close resources.
الدروس
المختبر والتمارين
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)
| ID | Outcome | Task |
|---|---|---|
| BEG-034-EX01 | 1, 2 | Create a data folder if missing, write three lines to data/notes.txt, read them back and print them with line numbers. |
| BEG-034-EX02 | 2, 4 | Write 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-EX03 | 3 | Explain 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"));thenList<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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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).