Monday, March 11, 2013

Java / Check if is directory

How to check if given file name is directory?
Use File class and isDirectory() method.
This method returns true if directory exists otherwise false

import java.io.File;

public class IsDirecory 
{
 public static void main(String[] args) 
 {
  File file = new File("readme.txt");
  boolean result = file.isDirectory();
  
  if( result == true )
   System.out.println( "It is a direcory" );
  else
   System.out.println( "Directory doesn't exists or isn't a directory" );
 }
}

No comments: