Check File Exists Java

Check File Exists Java

Many time when you work with file its required to check if file exits or not otherwise you many get file not found exception. Below is sample program to check file exists or not:

  • CheckFileExists.java
package com.javahonk;

import java.io.File;

public class CheckFileExists {

	public static void main(String[] args) {
		
		File file = new File("C:\\JavaHonk\\FileReadFile.txt");
		if(file.exists() && !file.isDirectory()) { 
			System.out.println("File exists");
		}else {
			System.out.println("File not exists");
		}
	}

}
  • Output:

Check File Exists Java

  • For more information about File API please refer oracle documentation here

Leave a Reply

Your email address will not be published. Required fields are marked *