Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java File isDirectory() Method

Java File isDirectory() Method

Java File Java File


isDirectory() is a method in Java's java.io.File an instance method provided by the class, used to determine whether the current File object represents a directory (folder). The method returns a boolean value (boolean), returning if the path points to a directory true; otherwise, returns false.

Method Syntax

public

boolean

isDirectory

(

)

Return Value


Usage examples

Basic Usage

Example

import

java.io.File

;

public

class

IsDirectoryExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a File object pointing to a directory

       

File

dir

=

new

File

(

"C:/example_directory"

)

;

       

       

// Check whether it is a directory

       

if

(

dir.

isDirectory

(

)

)

{

           

System

.

out

.

println

(

"This is a directory"

)

;

       

}

else

{

           

System

.

out

.

println

(

"This is not a directory or the directory does not exist"

)

;

       

}

   

}

}

Used together with the exists() method

In actual development, we usually first check whether the path exists, then determine whether it is a directory:

Example

File

file

=

new

File

(

"C:/example_path"

)

;

if

(

file.

exists

(

)

)

{

 

// First check whether the path exists

   

if

(

file.

isDirectory

(

)

)

{

       

System

.

out

.

println

(

"This is an existing directory"

)

;

   

}

else

{

       

System

.

out

.

println

(

"This is an existing file"

)

;

   

}

}

else

{

   

System

.

out

.

println

(

"The path does not exist"

)

;

}


Notes

  1. Path existence check:isDirectory() The method also returns for a non-existent path false, so you usually need to use the exists() method first to check whether the path exists.

  2. Symbolic links: in Unix/Linux systems, if the path is a symbolic link (symlink) pointing to a directory,isDirectory() will also return true.

  3. Permission issues: if the program does not have permission to access the path,isDirectory() will also return false, even if it is actually a directory.

  4. Relative and Absolute Paths: the method's behavior is not affected by the path type (relative or absolute); it only cares whether the path points to a directory.


Practical Application Scenarios

Scenario 1: Traverse directory contents

Example

File

directory

=

new

File

(

"C:/projects"

)

;

if

(

directory.

isDirectory

(

)

)

{

   

File

[

]

files

=

directory.

listFiles

(

)

;

   

for

(

File

file

:

files

)

{

       

System

.

out

.

println

(

file.

getName

(

)

)

;

   

}

}

Scenario 2: Check before creating a directory

Example

File

newDir

=

new

File

(

"C:/new_directory"

)

;

if

(

!

newDir.

isDirectory

(

)

)

{

   

boolean

created

=

newDir.

mkdir

(

)

;

   

if

(

created

)

{

       

System

.

out

.

println

(

"Directory created successfully"

)

;

   

}

}


Related methods

  1. isFile(): check whether it is a regular file
  2. exists(): check whether the path exists
  3. listFiles(): get files and subdirectories in a directory
  4. mkdir(): create a single-level directory
  5. mkdirs(): create multi-level directories

Summary

isDirectory() The method is a basic but important method in Java file operations; it helps us determine whether a path points to a directory. In practical applications, it usually needs to be combined with other file operation methods to build complete file-handling logic. Remember to first check whether the path exists, then determine its type, which can avoid logical confusion and errors.

Java File Java File