Chapter 21 Advanced 55 Questions

Practice Questions — File Handling in Java

← Back to Notes
8 Easy
10 Medium
7 Hard

Topic-Specific Questions

Question 1
Easy
What is the output of the following code?
File file = new File("test.txt");
System.out.println(file.exists());

Assume the file test.txt does NOT exist.

new File() creates a File object representing a path. It does not create the actual file.
false
Question 2
Easy
What is the output?
File file = new File("demo.txt");
file.createNewFile();
System.out.println(file.exists());
System.out.println(file.isFile());
System.out.println(file.isDirectory());
createNewFile() actually creates the file on disk.
true
true
false
Question 3
Easy
What is the output?
try (BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"))) {
    bw.write("Hello");
    bw.newLine();
    bw.write("World");
}
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
    System.out.println(br.readLine());
    System.out.println(br.readLine());
    System.out.println(br.readLine());
}
readLine() returns null when there are no more lines.
Hello
World
null
Question 4
Easy
What is the output?
FileWriter fw1 = new FileWriter("data.txt");
fw1.write("First");
fw1.close();

FileWriter fw2 = new FileWriter("data.txt");
fw2.write("Second");
fw2.close();

BufferedReader br = new BufferedReader(new FileReader("data.txt"));
System.out.println(br.readLine());
br.close();
FileWriter without the append flag truncates the file.
Second
Question 5
Easy
What is the output?
FileWriter fw1 = new FileWriter("data.txt");
fw1.write("First");
fw1.close();

FileWriter fw2 = new FileWriter("data.txt", true);
fw2.write("Second");
fw2.close();

BufferedReader br = new BufferedReader(new FileReader("data.txt"));
System.out.println(br.readLine());
br.close();
The second FileWriter uses append mode (true).
FirstSecond
Question 6
Medium
What is the output?
try (BufferedWriter bw = new BufferedWriter(new FileWriter("nums.txt"))) {
    for (int i = 1; i <= 3; i++) {
        bw.write(String.valueOf(i * 10));
        bw.newLine();
    }
}

try (BufferedReader br = new BufferedReader(new FileReader("nums.txt"))) {
    String line;
    int sum = 0;
    while ((line = br.readLine()) != null) {
        sum += Integer.parseInt(line);
    }
    System.out.println("Sum: " + sum);
}
Three numbers are written: 10, 20, 30.
Sum: 60
Question 7
Medium
What happens when this code runs?
try (BufferedReader br = new BufferedReader(new FileReader("nonexistent.txt"))) {
    System.out.println(br.readLine());
} catch (FileNotFoundException e) {
    System.out.println("Not found");
} catch (IOException e) {
    System.out.println("IO error");
}
FileNotFoundException is a subclass of IOException.
Not found
Question 8
Medium
What is the output?
Path path = Paths.get("demo.txt");
Files.writeString(path, "Hello NIO");

String content = Files.readString(path);
System.out.println(content);
System.out.println(content.length());

Files.deleteIfExists(path);

(Assume Java 11+)

Files.readString() reads the entire file as a single String.
Hello NIO
9
Question 9
Medium
What is the output?
Files.write(Paths.get("lines.txt"), 
    List.of("alpha", "beta", "gamma"));

List<String> lines = Files.readAllLines(Paths.get("lines.txt"));
System.out.println(lines.size());
System.out.println(lines.get(1));
List.of() creates a list with three elements. readAllLines() reads all lines into a List.
3
beta
Question 10
Medium
What is the output?
try (FileWriter fw = new FileWriter("test.txt")) {
    fw.write("ABC");
}

File file = new File("test.txt");
System.out.println(file.length());

try (FileReader fr = new FileReader("test.txt")) {
    System.out.println(fr.read());
    System.out.println(fr.read());
}
FileReader.read() returns the character's Unicode integer value. 'A' is 65.
3
65
66
Question 11
Hard
What is the output?
Files.write(Paths.get("data.txt"), 
    List.of("hello", "", "world", "", "java"));

long count = Files.lines(Paths.get("data.txt"))
    .filter(line -> !line.isEmpty())
    .count();
System.out.println(count);
Two of the five lines are empty strings.
3
Question 12
Hard
What is the output?
try (PrintWriter pw = new PrintWriter("out.txt")) {
    pw.printf("%d + %d = %d%n", 3, 4, 7);
    pw.printf("%d + %d = %d%n", 5, 6, 11);
}

try (BufferedReader br = new BufferedReader(new FileReader("out.txt"))) {
    br.readLine(); // skip first line
    String second = br.readLine();
    System.out.println(second);
}
The first readLine() reads and discards the first line.
5 + 6 = 11
Question 13
Medium
What is the difference between FileReader and FileInputStream? When would you use each?
Think about what kind of data each handles.
FileReader is a character stream that reads text data (characters), applying character encoding. FileInputStream is a byte stream that reads raw bytes without any encoding interpretation. Use FileReader for text files (.txt, .csv, .json) and FileInputStream for binary files (.jpg, .pdf, .exe).
Question 14
Hard
Why does Files.readAllLines() have a potential problem with very large files? What is the alternative?
Think about where the data goes when you read all lines.
Files.readAllLines() loads the entire file into a List<String> in memory. For a 2 GB log file, this would require at least 2 GB of heap space, likely causing OutOfMemoryError. The alternative is Files.lines(), which returns a lazy Stream<String> that reads lines on demand without loading the entire file into memory.
Question 15
Hard
Explain how try-with-resources works internally. What interface must a class implement to be used in it?
Think about what guarantees automatic closing.
A class must implement the AutoCloseable interface (which has a single method: close()). When the try block exits (normally or due to an exception), the JVM automatically calls close() on each resource declared in the parentheses, in reverse order of declaration. If both the try block and the close() method throw exceptions, the close() exception is added as a suppressed exception to the main exception.

Mixed & Application Questions

Question 1
Easy
What is the output?
File f = new File("mydir");
f.mkdir();
System.out.println(f.isDirectory());
System.out.println(f.isFile());
f.delete();
mkdir() creates a directory, not a file.
true
false
Question 2
Easy
What is the output?
try (PrintWriter pw = new PrintWriter("simple.txt")) {
    pw.println("Line 1");
    pw.println("Line 2");
}

try (Scanner sc = new Scanner(new File("simple.txt"))) {
    while (sc.hasNextLine()) {
        System.out.println(sc.nextLine());
    }
}
PrintWriter.println() writes a line with a newline. Scanner.nextLine() reads one line.
Line 1
Line 2
Question 3
Medium
What is the output?
try (BufferedWriter bw = new BufferedWriter(new FileWriter("count.txt"))) {
    for (int i = 5; i >= 1; i--) {
        bw.write(i + " ");
    }
}
try (BufferedReader br = new BufferedReader(new FileReader("count.txt"))) {
    System.out.println(br.readLine());
}
All numbers are written on a single line (no newLine() calls).
5 4 3 2 1
Question 4
Medium
What is the output?
Files.write(Paths.get("words.txt"), 
    List.of("apple", "banana", "cherry"));

String result = Files.lines(Paths.get("words.txt"))
    .map(s -> s.substring(0, 1).toUpperCase())
    .reduce("", (a, b) -> a + b);

System.out.println(result);
Each word's first character is extracted and capitalized, then all are concatenated.
ABC
Question 5
Medium
What is the output?
Path path = Paths.get("info.txt");
Files.writeString(path, "Hello\nWorld\n");

List<String> lines = Files.readAllLines(path);
System.out.println(lines.size());
System.out.println(lines);
The string contains two newline characters, creating two lines of content.
2
[Hello, World]
Question 6
Hard
What is the output?
try (BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"))) {
    bw.write("Line1");
    bw.newLine();
    bw.write("Line2");
    // Note: no newLine() after Line2
}

int lineCount = 0;
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
    while (br.readLine() != null) {
        lineCount++;
    }
}
System.out.println(lineCount);
readLine() reads until newline or end of file. The last line has no trailing newline.
2
Question 7
Hard
What is the output?
try (PrintWriter pw = new PrintWriter("matrix.txt")) {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            pw.print(i * j + " ");
        }
        pw.println();
    }
}

try (BufferedReader br = new BufferedReader(new FileReader("matrix.txt"))) {
    System.out.println(br.readLine());
    br.readLine(); // skip second line
    System.out.println(br.readLine());
}
Trace the multiplication table. The second line is skipped.
1 2 3
3 6 9
Question 8
Hard
What is the output?
Files.write(Paths.get("numbers.txt"), 
    List.of("10", "20", "abc", "30", "xyz"));

int sum = 0;
int errors = 0;
for (String line : Files.readAllLines(Paths.get("numbers.txt"))) {
    try {
        sum += Integer.parseInt(line);
    } catch (NumberFormatException e) {
        errors++;
    }
}
System.out.println("Sum: " + sum);
System.out.println("Errors: " + errors);
"abc" and "xyz" cannot be parsed as integers.
Sum: 60
Errors: 2
Question 9
Easy
What is the purpose of the newLine() method in BufferedWriter?
Think about what happens on different operating systems.
newLine() writes a platform-independent line separator. On Windows, the line separator is "\r\n". On Linux/Mac, it is "\n". Using newLine() instead of hardcoding "\n" makes your code work correctly across all operating systems.
Question 10
Medium
What is the difference between Files.readAllLines() and Files.lines()?
One loads everything into memory at once. The other processes lazily.
Files.readAllLines() reads the entire file into a List<String> (eager loading -- all in memory). Files.lines() returns a Stream<String> that reads lines lazily (on demand). Use readAllLines() for small files when you need random access. Use Files.lines() for large files or when using stream operations.

Multiple Choice Questions

MCQ 1
Which class is used to check if a file exists on disk?
  • A. FileReader
  • B. FileWriter
  • C. File
  • D. Scanner
Answer: C
C is correct. The File class provides the exists() method to check if a file or directory exists. FileReader and FileWriter are for reading/writing, and Scanner is for parsing input.
MCQ 2
What does BufferedReader.readLine() return when it reaches the end of the file?
  • A. An empty string ""
  • B. The string "EOF"
  • C. null
  • D. -1
Answer: C
C is correct. readLine() returns null at end of file. An empty string is returned for blank lines in the file. -1 is returned by read() (single character), not readLine().
MCQ 3
What does try-with-resources guarantee?
  • A. The file will be created automatically
  • B. Resources will be closed automatically when the block exits
  • C. No exceptions will occur
  • D. The file will never be corrupted
Answer: B
B is correct. try-with-resources automatically calls close() on all declared resources when the block exits, whether normally or due to an exception.
MCQ 4
Which stream type should you use to copy an image file?
  • A. FileReader / FileWriter
  • B. BufferedReader / BufferedWriter
  • C. FileInputStream / FileOutputStream
  • D. Scanner / PrintWriter
Answer: C
C is correct. Images are binary files. Character streams (Reader/Writer) apply character encoding that corrupts binary data. Byte streams (InputStream/OutputStream) handle raw bytes correctly.
MCQ 5
How do you open a FileWriter in append mode?
  • A. new FileWriter("file.txt", "append")
  • B. new FileWriter("file.txt", true)
  • C. new FileWriter("file.txt").append()
  • D. FileWriter.append("file.txt")
Answer: B
B is correct. The second parameter true enables append mode. Without it (or with false), the file is truncated on opening.
MCQ 6
Which method reads the entire file content as a single String in Java 11+?
  • A. Files.readAllLines()
  • B. Files.readString()
  • C. Files.lines()
  • D. Files.read()
Answer: B
B is correct. Files.readString() (Java 11+) reads the entire file into one String. readAllLines() returns a List of lines. lines() returns a Stream. There is no Files.read() method for text.
MCQ 7
What interface must a class implement to be used in try-with-resources?
  • A. Serializable
  • B. Closeable only
  • C. AutoCloseable
  • D. Readable
Answer: C
C is correct. AutoCloseable is the required interface. Closeable extends AutoCloseable, so classes implementing Closeable also work. But the actual requirement is AutoCloseable.
MCQ 8
What is the advantage of BufferedReader over FileReader?
  • A. BufferedReader can read binary files
  • B. BufferedReader uses an internal buffer for better performance
  • C. BufferedReader can write to files
  • D. BufferedReader does not require closing
Answer: B
B is correct. BufferedReader reads large chunks into an internal buffer (default 8192 chars), reducing the number of disk I/O operations. FileReader reads from disk on every read() call, which is much slower.
MCQ 9
What does Files.lines() return?
  • A. List<String>
  • B. String[]
  • C. Stream<String>
  • D. Iterator<String>
Answer: C
C is correct. Files.lines() returns a Stream<String> that lazily reads lines from the file. This stream must be closed after use (try-with-resources).
MCQ 10
What exception is thrown when you try to open a file that does not exist with FileReader?
  • A. IOException
  • B. FileNotFoundException
  • C. NullPointerException
  • D. IllegalArgumentException
Answer: B
B is correct. FileNotFoundException is thrown when the specified file does not exist. It is a subclass of IOException, so catch (IOException e) would also catch it.
MCQ 11
In try-with-resources with multiple resources, in what order are they closed?
  • A. In the order they were declared
  • B. In reverse order of declaration
  • C. Alphabetical order
  • D. Random order
Answer: B
B is correct. Resources are closed in reverse order of declaration. This is important because later resources may depend on earlier ones (e.g., a BufferedWriter wrapping a FileWriter), so the outer wrapper must be closed first.
MCQ 12
What happens if both the try block and the close() method throw exceptions in try-with-resources?
  • A. The try exception is lost
  • B. The close exception is lost
  • C. The close exception is added as a suppressed exception to the try exception
  • D. Both exceptions are thrown simultaneously
Answer: C
C is correct. The exception from the try block is the primary exception that propagates. The exception from close() is added as a suppressed exception, retrievable via getSuppressed(). This ensures no exception is silently lost.
MCQ 13
Why is Scanner slower than BufferedReader for reading large files?
  • A. Scanner cannot read files at all
  • B. Scanner uses regex parsing internally for every token
  • C. Scanner does not use buffering
  • D. Scanner reads binary data instead of text
Answer: B
B is correct. Scanner uses regular expressions internally to parse tokens, which adds overhead. BufferedReader.readLine() simply reads bytes until a newline, which is significantly faster for plain line-by-line reading.
MCQ 14
What is a suppressed exception in the context of try-with-resources?
  • A. An exception that is ignored
  • B. An exception thrown by close() that is attached to the primary exception
  • C. An exception that does not have a stack trace
  • D. An exception that occurs in the finally block
Answer: B
B is correct. When the try block throws an exception and then close() also throws an exception, the close exception is added as a suppressed exception to the primary one using addSuppressed(). Retrieve it with exception.getSuppressed().
MCQ 15
Which NIO class provides static methods like readAllLines(), write(), and exists()?
  • A. Path
  • B. Paths
  • C. Files
  • D. File
Answer: C
C is correct. The Files class (java.nio.file.Files) provides static utility methods for file operations. Path represents a file path. Paths creates Path objects. File is the older java.io class.

Coding Challenges

Challenge 1: Word Counter

Easy
Write a program that reads a text file and counts the total number of lines, words, and characters. Print the results in a formatted table.
Sample Input
Create a file with: "Hello World\nJava is great\nFile handling rocks"
Sample Output
Lines: 3 Words: 8 Characters: 42
Use BufferedReader with try-with-resources. Handle FileNotFoundException.
import java.io.*;

public class WordCounter {
    public static void main(String[] args) {
        // Create test file
        try (PrintWriter pw = new PrintWriter("sample.txt")) {
            pw.println("Hello World");
            pw.println("Java is great");
            pw.println("File handling rocks");
        } catch (FileNotFoundException e) {
            System.out.println("Cannot create file");
            return;
        }

        int lines = 0, words = 0, chars = 0;
        try (BufferedReader br = new BufferedReader(new FileReader("sample.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                lines++;
                chars += line.length();
                String[] tokens = line.trim().split("\\s+");
                if (!tokens[0].isEmpty()) {
                    words += tokens.length;
                }
            }
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
            return;
        }
        System.out.println("Lines: " + lines);
        System.out.println("Words: " + words);
        System.out.println("Characters: " + chars);
    }
}

Challenge 2: CSV Processor

Medium
Write a program that reads a CSV file containing student names and three subject marks, calculates the average for each student, and writes the results to a new file with a pass/fail status (pass if average >= 40).
Sample Input
Arjun,85,92,78\nSneha,35,28,42\nVikram,91,88,95
Sample Output
Arjun,85,92,78,85.0,PASS\nSneha,35,28,42,35.0,FAIL\nVikram,91,88,95,91.3,PASS
Use BufferedReader for reading and PrintWriter for writing. Handle NumberFormatException for invalid data.
import java.io.*;

public class CSVProcessor {
    public static void main(String[] args) {
        // Create input file
        try (PrintWriter pw = new PrintWriter("students.csv")) {
            pw.println("Arjun,85,92,78");
            pw.println("Sneha,35,28,42");
            pw.println("Vikram,91,88,95");
        } catch (FileNotFoundException e) {
            return;
        }

        try (
            BufferedReader br = new BufferedReader(new FileReader("students.csv"));
            PrintWriter pw = new PrintWriter(new FileWriter("results.csv"))
        ) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(",");
                String name = parts[0];
                try {
                    int m1 = Integer.parseInt(parts[1]);
                    int m2 = Integer.parseInt(parts[2]);
                    int m3 = Integer.parseInt(parts[3]);
                    double avg = (m1 + m2 + m3) / 3.0;
                    String status = avg >= 40 ? "PASS" : "FAIL";
                    pw.printf("%s,%d,%d,%d,%.1f,%s%n", name, m1, m2, m3, avg, status);
                } catch (NumberFormatException e) {
                    System.out.println("Invalid data for: " + name);
                }
            }
            System.out.println("Results written to results.csv");
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Challenge 3: File Search Tool

Medium
Write a program that reads a text file and searches for a given keyword. Print each line that contains the keyword along with its line number. Also print the total number of occurrences.
Sample Input
Search for "Java" in a file with multiple lines mentioning Java.
Sample Output
Line 2: Java is a popular language. Line 5: Java supports OOP. Found 2 occurrences in 2 lines.
Use BufferedReader. Perform case-insensitive search. Count total occurrences (not just lines).
import java.io.*;

public class FileSearch {
    public static void main(String[] args) {
        try (PrintWriter pw = new PrintWriter("article.txt")) {
            pw.println("Programming is fun.");
            pw.println("Java is a popular language.");
            pw.println("Python is also widely used.");
            pw.println("Many companies use both.");
            pw.println("Java supports OOP and multithreading.");
        } catch (FileNotFoundException e) {
            return;
        }

        String keyword = "Java";
        int lineCount = 0, totalOccurrences = 0;
        try (BufferedReader br = new BufferedReader(new FileReader("article.txt"))) {
            String line;
            int lineNum = 0;
            while ((line = br.readLine()) != null) {
                lineNum++;
                String lowerLine = line.toLowerCase();
                String lowerKey = keyword.toLowerCase();
                int index = 0;
                int count = 0;
                while ((index = lowerLine.indexOf(lowerKey, index)) != -1) {
                    count++;
                    index += lowerKey.length();
                }
                if (count > 0) {
                    lineCount++;
                    totalOccurrences += count;
                    System.out.println("Line " + lineNum + ": " + line);
                }
            }
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
            return;
        }
        System.out.println("Found " + totalOccurrences + " occurrences in " + lineCount + " lines.");
    }
}

Challenge 4: File Merger with NIO

Medium
Write a program using Java NIO that creates two text files, then merges their content into a third file. Each line in the merged file should be prefixed with the source file name.
Sample Input
File1: "Hello\nWorld", File2: "Java\nRocks"
Sample Output
[file1.txt] Hello\n[file1.txt] World\n[file2.txt] Java\n[file2.txt] Rocks
Use java.nio.file.Files and Path. Use Streams API to process lines.
import java.nio.file.*;
import java.io.IOException;
import java.util.*;
import java.util.stream.*;

public class FileMerger {
    public static void main(String[] args) throws IOException {
        Path file1 = Paths.get("file1.txt");
        Path file2 = Paths.get("file2.txt");
        Path merged = Paths.get("merged.txt");

        Files.write(file1, List.of("Hello", "World"));
        Files.write(file2, List.of("Java", "Rocks"));

        List<String> mergedLines = new ArrayList<>();

        try (Stream<String> lines = Files.lines(file1)) {
            lines.map(line -> "[file1.txt] " + line)
                 .forEach(mergedLines::add);
        }
        try (Stream<String> lines = Files.lines(file2)) {
            lines.map(line -> "[file2.txt] " + line)
                 .forEach(mergedLines::add);
        }

        Files.write(merged, mergedLines);

        System.out.println("Merged file contents:");
        Files.readAllLines(merged).forEach(System.out::println);

        Files.deleteIfExists(file1);
        Files.deleteIfExists(file2);
        Files.deleteIfExists(merged);
    }
}

Challenge 5: Log File Analyzer

Hard
Write a program that reads a log file where each line has the format 'LEVEL: message' (levels: INFO, WARN, ERROR). Count occurrences of each level, find all ERROR messages, and write a summary report to a new file.
Sample Input
INFO: Server started\nWARN: Low memory\nERROR: Null pointer\nINFO: Request processed\nERROR: Timeout
Sample Output
Summary: INFO=2, WARN=1, ERROR=2\nErrors:\n 1. Null pointer\n 2. Timeout
Use BufferedReader for reading, PrintWriter for writing. Handle malformed lines gracefully.
import java.io.*;
import java.util.*;

public class LogAnalyzer {
    public static void main(String[] args) {
        try (PrintWriter pw = new PrintWriter("app.log")) {
            pw.println("INFO: Server started");
            pw.println("WARN: Low memory detected");
            pw.println("ERROR: NullPointerException in UserService");
            pw.println("INFO: Request processed successfully");
            pw.println("ERROR: Connection timeout to database");
            pw.println("INFO: Scheduled task completed");
            pw.println("WARN: Deprecated API usage");
        } catch (FileNotFoundException e) {
            return;
        }

        Map<String, Integer> counts = new LinkedHashMap<>();
        counts.put("INFO", 0);
        counts.put("WARN", 0);
        counts.put("ERROR", 0);
        List<String> errors = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new FileReader("app.log"))) {
            String line;
            while ((line = br.readLine()) != null) {
                int colonIndex = line.indexOf(':');
                if (colonIndex == -1) continue;
                String level = line.substring(0, colonIndex).trim();
                String message = line.substring(colonIndex + 1).trim();
                counts.merge(level, 1, Integer::sum);
                if ("ERROR".equals(level)) {
                    errors.add(message);
                }
            }
        } catch (IOException e) {
            System.out.println("Read error: " + e.getMessage());
            return;
        }

        try (PrintWriter pw = new PrintWriter("report.txt")) {
            pw.println("=== Log Analysis Report ===");
            pw.println();
            for (Map.Entry<String, Integer> entry : counts.entrySet()) {
                pw.printf("%-8s: %d%n", entry.getKey(), entry.getValue());
            }
            pw.println();
            pw.println("Error Details:");
            for (int i = 0; i < errors.size(); i++) {
                pw.printf("  %d. %s%n", i + 1, errors.get(i));
            }
            System.out.println("Report written to report.txt");
        } catch (FileNotFoundException e) {
            System.out.println("Write error: " + e.getMessage());
        }
    }
}

Need to Review the Concepts?

Go back to the detailed notes for this chapter.

Read Chapter Notes

Want to learn Java with a live mentor?

Explore our Java Masterclass