Java File getName() Method
getName() is a method in Java's java.io.File an instance method provided by the class, used to obtain the name (excluding path information) of a file or directory. This method returns a string representing the name part of the file object.
Method Syntax
public
String
getName
(
)
Return Value
Returns a string representing the name of the file or directory (excluding path information).
Use Cases
getName() The method is especially useful in the following situations:
- When you only need the file name and do not care about its path
- To extract file names in file listing operations
- When you need to display or record file names
- When performing string operations related to file names
Example Code
Basic Usage Example
Example
import
java.io.File
;
public
class
GetNameExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a File object
File
file
=
new
File
(
"C:/Users/Example/Documents/report.txt"
)
;
// Use getName() to get the file name
String
fileName
=
file.
getName
(
)
;
System
.
out
.
println
(
"Full path: "
file.
getPath
(
)
)
;
System
.
out
.
println
(
"File name: "
fileName
)
;
}
}
Output result
完整路径: C:\Users\Example\Documents\report.txt
文件名: report.txt
Example of handling directories
Example
import
java.io.File
;
public
class
DirectoryNameExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a File object pointing to a directory
File
directory
=
new
File
(
"C:/Program Files/Java"
)
;
// Get the directory name
String
dirName
=
directory.
getName
(
)
;
System
.
out
.
println
(
"Directory name: "
dirName
)
;
}
}
Output result
目录名: Java
Technical Details
Implementation principle
getName() The working principle of the method is:
- First, get the complete path string
- Then find the last path separator ('' on Windows, '/' on Unix-like systems)
- Return the part of the string after the separator
Path separator handling
Java automatically handles path separator differences across operating systems, so no matter which separator you use,getName() works correctly.
Notes
- If both the
FileIf the object represents a root directory (such as "C:" or "/"),getName()will return an empty string "" - The method does not check whether the file or directory actually exists
- The returned name includes the file extension (if any)
- For a relative path, only the last part of the path is returned
Comparison with Other Methods
getName() vs getPath()
getName(): returns only the name part of the file or directorygetPath(): returns the complete path string used when the File object was created
getName() vs getAbsolutePath()
getName(): returns only the name partgetAbsolutePath(): returns the complete absolute path
Practical Application Examples
Batch processing of file extensions
Example
import
java.io.File
;
public
class
FileExtensionExample
{
public
static
void
main
(
String
[
]
args
)
{
File
file
=
new
File
(
"data/archive.zip"
)
;
String
name
=
file.
getName
(
)
;
// Get the file extension
int
dotIndex
=
name.
lastIndexOf
(
'.'
)
;
if
(
dotIndex
0
)
{
String
extension
=
name.
substring
(
dotIndex
1
)
;
System
.
out
.
println
(
"File extension: "
extension
)
;
}
}
}
Output result
文件扩展名: zip
Traverse a directory and get file names
Example
import
java.io.File
;
public
class
ListFilesExample
{
public
static
void
main
(
String
[
]
args
)
{
File
directory
=
new
File
(
"C:/Users/Example/Downloads"
)
;
// List all files in a directory
File
[
]
files
=
directory.
listFiles
(
)
;
if
(
files
!=
null
)
{
System
.
out
.
println
(
"Files in the download directory:"
)
;
for
(
File
file
:
files
)
{
if
(
file.
isFile
(
)
)
{
System
.
out
.
println
(
"- "
file.
getName
(
)
)
;
}
}
}
}
}
By mastering getName() method, you can handle the name information of files and directories more conveniently, providing basic support for file operations.
Java File