Sunday, 11 June 2017

Java Tutorial : Java IO (Java File or Dir exists | How to check if a file or Dir exists) ~ foundjava


Click here to watch in Youtube :
FileDemo1.java
import java.io.File;

/*
 *Once you have instantiated a File object we can check
 *if the corresponding file/dir actually exists already.
 */
public class FileDemo1
{

    public static void main(String[] args)
    {
        File file = new File("D:/work/myfile.txt");
        /*
         * Tests whether the file or directory denoted by
         * this abstract pathname exists.
         */
        boolean isExist = file.exists();
        System.out.println(file.getAbsolutePath() + " is exist? = " + isExist);
    }

}
Output
D:\work\myfile.txt is exist? = true

FileDemo2.java
import java.io.File;

/*
 *Once you have instantiated a File object we can check
 *if the corresponding file/dir actually exists already.
 */
public class FileDemo2
{

    public static void main(String[] args)
    {
        File file = new File("D:/work/");
        /*
         * Tests whether the file or directory denoted by
         * this abstract pathname exists.
         */
        boolean isExist = file.exists();
        System.out.println(file.getAbsolutePath() + " is exist? = " + isExist);
    }

}
Output
D:\work is exist? = true
Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment