Check File Exists in Java: Explanation & Practice

Check if a file exists before operations

Problem summary

Check if a file exists and print appropriate message.

Starter code

import java.io.*;

public class Main {
    public static void main(String[] args) {
        // Check if "data.txt" exists
        // Print: File exists OR File not found
    }
}

Expected output and test cases

  • File found
    File exists

Hints

  1. Use File.exists() method
  2. Create File object first
  3. Can also check isFile() vs isDirectory()

Related File I/O exercises

Practice all File I/O exercises · Run this idea in the Java compiler