Java File mkdir() Method
mkdir() The method is in Java's File an instance method provided by the class, used to create a single-level directory. The method attempts to create the directory represented by this File object and returns a boolean indicating whether the operation succeeded.
Method Syntax
public
boolean
mkdir
(
)
Return Value
true: if the directory is created successfullyfalse: if the directory already exists or creation fails
Use Cases
mkdir() The method is suitable for the following scenarios:
Create a single directory
When you only need to create a single separate directory, you can use this method.
Simple directory creation needs
Suitable for simple scenarios that do not need to create multi-level directories at the same time.
Method Characteristics
Single-level directory creation
mkdir() Can only create a single-level directory. If the parent directory of the directory to be created does not exist, the method fails.
Non-atomic operation
Directory creation is not an atomic operation and may fail during the process.
Security Considerations
You should check whether you have sufficient permissions before creating a directory.
Usage examples
Basic Usage
Example
import
java.io.File
;
public
class
MkdirExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a File object representing the new directory
File
dir
=
new
File
(
"exampleDir"
)
;
// Attempt to create the directory
boolean
result
=
dir.
mkdir
(
)
;
if
(
result
)
{
System
.
out
.
println
(
"Directory created successfully"
)
;
}
else
{
System
.
out
.
println
(
"Directory creation failed (may already exist or no permission)"
)
;
}
}
}
Check whether the parent directory exists
Example
import
java.io.File
;
public
class
MkdirWithParentCheck
{
public
static
void
main
(
String
[
]
args
)
{
File
dir
=
new
File
(
"parentDir/childDir"
)
;
// Check whether the parent directory exists
if
(
!
dir.
getParentFile
(
)
.
exists
(
)
)
{
System
.
out
.
println
(
"Parent directory does not exist, mkdir() will fail"
)
;
}
boolean
result
=
dir.
mkdir
(
)
;
System
.
out
.
println
(
"Creation result: "
result
)
;
}
}
Difference from mkdirs()
Limitations of mkdir()
mkdir() Can only create a single-level directory; if any parent directory in the path does not exist, creation fails.
Advantages of mkdirs()
mkdirs() The method can create multi-level directories (including all non-existent parent directories).
Example
File
dir
=
new
File
(
"a/b/c"
)
;
boolean
result
=
dir.
mkdirs
(
)
;
// Will create a, a/b, a/b/c all directories
Best Practices
Check whether a directory exists
Before attempting to create a directory, first check whether it already exists:
Example
if
(
!
dir.
exists
(
)
)
{
dir.
mkdir
(
)
;
}
Handle creation failure
Example
if
(
!
dir.
mkdir
(
)
)
{
// Handle failure case
System
.
err
.
println
(
"Cannot create directory: "
dir.
getAbsolutePath
(
)
)
;
}
Consider using mkdirs()
If you may need to create multi-level directories, consider using mkdirs() instead of mkdir().
Common questions
Why does mkdir() return false?
Possible reasons:
- Directory already exists
- Insufficient permissions
- Parent directory does not exist
- Invalid pathname
How to check the reason for failure?
You can check through the following methods:
Example
if
(
dir.
exists
(
)
)
{
System
.
out
.
println
(
"Directory already exists"
)
;
}
else
if
(
!
dir.
getParentFile
(
)
.
canWrite
(
)
)
{
System
.
out
.
println
(
"No write permission"
)
;
}
else
if
(
!
dir.
getParentFile
(
)
.
exists
(
)
)
{
System
.
out
.
println
(
"Parent directory does not exist"
)
;
}
Summary
mkdir() It is the basic method for creating single-level directories in Java; it is simple to use but has limited functionality. For more complex directory creation needs, you should consider using mkdirs() method. In practical applications, you should always check the method's return value and properly handle possible failures.
Java File