Java Example – Getting the Parent Directory of a File
The following example demonstrates using the file.getParent() method of the File class to get the parent directory of a file:
Main.java File
import
java
.
io
.
File
;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
File
file
=
new
File
(
"
C:/File/demo.txt
"
)
;
String
strParentDirectory
=
file
.
getParent
(
)
;
System
.
out
.
println
(
"
The parent directory of the file is:
"
strParentDirectory
)
;
}
}
The output of the above code is:
文件的上级目录为 : File
Java Examples