Skip to content
Learn Netverks

Lesson

Step 26/36 72% through track

file-io-basics

File I/O basics

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches File I/O basics: the syntax, APIs, and habits you need before advancing in Java.

Teams ship File I/O basics on every Java codebase—skipping it leaves gaps in debugging and code reviews.

You will apply File I/O basics in contexts like: Spring Boot APIs, banking systems, Android (with Kotlin), and batch/data pipelines on the JVM.

Write Java with a public class (lessons use Main), click Run on server—the dev runner runs javac then java; fix compile errors from stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Java I/O lives in java.io (classic streams) and java.nio.file (modern Paths API). Prefer Files.readString / writeString for simple text in Java 11+.

Try-with-resources

try (BufferedReader reader = Files.newBufferedReader(path)) {
    String line = reader.readLine();
}

Auto-closes resources—implements AutoCloseable.

Playground note

The sandbox may restrict filesystem paths. In production, validate paths, handle IOException, and never trust user-supplied filenames for writes.

Important interview questions and answers

  1. Q: try-with-resources benefit?
    A: Guaranteed close even on exception—prevents descriptor leaks.
  2. Q: nio.file vs java.io?
    A: NIO.2 Paths API is simpler for files; streams still used for sockets and legacy APIs.

Self-check

  1. Which interface enables try-with-resources?
  2. Why catch IOException separately from RuntimeException?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Path vs File legacy?
  • Try-with-resources file?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump