By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
File I/O in Java involves reading from and writing to files using classes like FileReader, FileWriter, BufferedReader, and PrintWriter. Mastering these concepts is crucial for handling data persistence, logging, and configuration management in applications. Incorrect usage can lead to data loss, application crashes, or security vulnerabilities. For instance, improper file handling can cause data corruption, making your application unreliable.
java FileReader fileReader = new FileReader("example.txt"); BufferedReader bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } bufferedReader.close();
Common Pitfall: Forgetting to close the reader can lead to resource leaks.
Writing to a File using FileWriter and PrintWriter
java FileWriter fileWriter = new FileWriter("output.txt"); PrintWriter printWriter = new PrintWriter(fileWriter); printWriter.println("Hello, World!"); printWriter.close();
Common Pitfall: Not closing the writer can result in incomplete writes.
Handling Exceptions
java try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); }
Common Pitfall: Ignoring exceptions can hide critical issues.
Appending to a File
java FileWriter fileWriter = new FileWriter("output.txt", true); PrintWriter printWriter = new PrintWriter(fileWriter); printWriter.println("Appended text"); printWriter.close();
Experts view file I/O as a critical component of application stability and performance. They focus on efficient resource management, proper exception handling, and choosing the right stream type for the task. Instead of memorizing methods, they understand the underlying principles of buffering and formatting.
Exam trap: Questions that involve long methods without resource management.
The mistake: Ignoring exceptions.
Exam trap: Code snippets without exception handling.
The mistake: Using the wrong stream type.
Exam trap: Questions that mix stream types.
The mistake: Forgetting the append flag.
Scenario: You need to read a configuration file and print its contents. Question: Write the code to read "config.txt" and print each line. Solution:
try (BufferedReader br = new BufferedReader(new FileReader("config.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); }
Answer: The code reads and prints each line of "config.txt". Why it works: BufferedReader efficiently reads the file line by line.
Scenario: You need to log user actions to a file. Question: Write the code to append "User logged in" to "log.txt". Solution:
try (FileWriter fw = new FileWriter("log.txt", true); PrintWriter pw = new PrintWriter(fw)) { pw.println("User logged in"); } catch (IOException e) { e.printStackTrace(); }
Answer: The code appends "User logged in" to "log.txt". Why it works: FileWriter with the append flag adds data to the file.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.