Sunday, 11 June 2017

Java Tutorial : Java IO (Java File - How to check path is file or dir) ~ foundjava


Click here to watch in Youtube : 

FileDemo1.java
import java.io.File;

/*
 * Check if Path is File or Directory
 */
public class FileDemo1
{

    public static void main(String[] args)
    {
        File file = new File("D:/work/myfile.txt");
        /*
         * Tests whether the file denoted by this abstract
         * pathname is a normal file.
         * 
         * Returns: true if and only if the file denoted by
         * this abstract pathname exists and is a normal
         * file; false otherwise
         */
        boolean isFile = file.isFile();
        System.out.println("isFile = " + isFile);
    }

}
Output
isFile = true
FileDemo2.java
import java.io.File;

/*
 * Check if Path is File or Directory
 */
public class FileDemo2
{

    public static void main(String[] args)
    {
        File file = new File("D:/work");
        /*
         * Tests whether the file denoted by this abstract
         * pathname is a directory.
         * 
         * Returns: true if and only if the file denoted by
         * this abstract pathname exists and is a directory;
         * false otherwise.
         */
        boolean isDir = file.isDirectory();
        System.out.println("isDir = " + isDir);
    }

}
Output
isDir = true

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment