Home Tutorial Java Core Files Java file exist

 
 

Share on Google+Share on Google+
Java file exist
Posted on: April 3, 2006 at 12:00 AM
Advertisement
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.
Advertisement

Related Tags for Java file exist:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 3, 2006

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com