How to check if a file exists in Java?

Example with the Java IO File.exists().

package com.nourit.javaIO;

import java.io.File;

public class FileExistExample {

  public static void main(String[] args) {
      File file = new File("D:\\nour-it.txt");

      if (file.exists()) {
        System.out.println("File: "+file.getName()+" found!");
      } else {
        System.out.println("File not found.");
      }
  }
}

Output:

File: nour-it.txt found!

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*