ADS

Check Whether A File Is A Directory Or File

An example on checking whether a file is directory or a normal file in Java using Java IO.


Check now!


import java.io.*;
class CheckFile
{

public static void main(String args[])
{
// Create a file object for a [directory (here)]
File file=new File("C:/java");

// If file is a directory
if(file.isDirectory())

// Print then.
System.out.println(file.getName()+" is a directory");

// If not a directory, then obviously a file
else System.out.println(file.getName()+" is a file");

/* You can alternatively use.. */

// Check if file object represents a normal file
if(file.isFile())

// Print then.
System.out.println(file.getName()+" is a file");

// If not a file, then will be a dir
else System.out.println(file.getName()+" is a directory");
}

}

Output


java is a directory
java is a directory

Any of the two methods can be used.

Also see my post on Removing a directory in Java easily

Subscribe to receive free email updates:

ADS