We are moved to new domain
Click -> www.ehowtonow.com
Wednesday, 13 May 2015

How to check file existence in Java

Following example shows how to check file existence using file.exists() method in Java.

exists
public boolean exists()
Tests whether the file or directory denoted by this abstract pathname exists.
Returns:
    true if and only if the file or directory denoted by this abstract pathname exists; false otherwise
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file or directory

Sample Program
CheckFileExist.java
package com.javatutorialcorner.file;

import java.io.File;

public class CheckFileExist {

 public static void main(String[] args) {
  File jtcFile = new File("/home/annamalai/workspace/File/jtc.txt");
  System.out.println(jtcFile.exists());

 }

}

Result
The above code will produce the following output if file exist in specified directory.

true

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to check file existence in Java Rating: 5 Reviewed By: eHowToNow