Java file exist


 

Java file exist

In this section, you will learn how to check the existence of the particular file.

In this section, you will learn how to check the existence of the particular file.

Java file exist 

In this section, you will learn how to check the existence of the particular file.

Description of code:

This is a very simple task. Here we have to check whether the specified file or directory exists or not. For this, we have created an object of class File and used exists() method. This method checks the existence of the file and returns true if the files exists otherwise returns false.

exists(): It checks whether the file or directory denoted by the pathname exists or not.

Here is the code:

import java.io.*;

public class FileExists{
  public static void main(String args[]){
    File file=new File("C:/roseindia.txt");
    boolean exists = file.exists();
    if (exists) {
     System.out.println("File or Directory exist.");
    }
    else{
     System.out.println("File or Directory does not exists.");
    }
  }
}

In the above code, method exists() is used. This method checks whether the file exists or not.

Output

File or Directory does not exists.

Ads