CodeGym
AI Academy
Promotion
CodeGym University
Learning
Courses
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme

Lessons

  • Reviews
  • About us
Start
Start learning
Start learning now
  • My Progress
  • Courses
  • University
  • Quest Map
  • Lessons
  • I/O errors: IOException and FileNotFoundException

    Kotlin SELF
    Level 45, Lesson 0
    We break down why file operations on the JVM “break”, why checking exists() does not guarantee success, how the “umbrella” IOException differs from FileNotFoundException, how to structure try/ catch correctly (from specific to general), and what to print for diagnostics (operation, path, and the exists/ isFile/ isDirectory properties) so debugging does not turn into guesswork.
    Available
  • Pre-checks before reading and writing: existence, type, permissions, directories

    Kotlin SELF
    Level 45, Lesson 1
    We go through practical pre-checks for files in Kotlin/JVM: how to make sure the path exists, that it is a file (not a directory), and that it is readable before reading ( exists(), isFile, canRead()), and how to prepare the parent directory before writing via parentFile and mkdirs(). Separately: quick path diagnostics and common mistakes (checking only exists(), ignoring the result of mkdirs(), the illusion of guarantees from canWrite()).
    Available
  • try/catch around file operations

    Kotlin SELF
    Level 45, Lesson 2
    We break down why, for file operations in Kotlin, you cannot rely only on checks like exists() and why you still need try/ catch around real I/O. We learn to make a “narrow” try around readText()/ writeText(), use try as an expression, choose a function contract ( T?, Boolean, or rethrowing IOException), write useful error messages, order multiple catch blocks correctly, and close resources via use {}.
    Available
  • Atomic write: temp file + renameTo as protection against partial writes

    Kotlin SELF
    Level 45, Lesson 3
    We walk through a practical “atomic write” protocol for saving important data to a file: why a direct call to writeText() during overwrite can leave a “truncated” file after a failure, how to write the new version into a temp file next to the target and then “publish” the result via renameTo(). We’ll build an atomicWriteText function with try/ finally, discuss the trade-offs of deleting the target, and cover common mistakes (temp not next to the target, not checking the renameTo result, catching everything indiscriminately, etc.).
    Available
  • Backup and rotation: keep previous versions before replacing a file

    Kotlin SELF
    Level 45, Lesson 4
    We’ll break down why you need a backup on top of atomic write, how to make the simplest .bak copy before replacing a file, why rotating copies must be done “from the end”, and how to assemble a safe save protocol: rotateBackups → atomicWriteText. At the end — a process diagram in mermaid and a review of typical mistakes (order of actions, ignoring renameTo, infinite copies).
    Available
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning, and a Java developer’s career.
Follow us
Interface language
English
Deutsch Español हिन्दी Français Português Polski বাংলা 简体中文 मराठी தமிழ் Italiano Bahasa Indonesia 繁體中文 Nederlands 日本語 한국어 Bulgarian Danish Hungarian Basa Jawa Malay Norwegian Romanian Swedish Telugu Thai Українська Filipino Turkish Azərbaycan Русский Vietnamese
Programmers Are Made, Not Born © 2026 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2026 CodeGym