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

Java File getName() Method

Java File getName() Method

Java File Java File

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:

  1. When you only need the file name and do not care about its path
  2. To extract file names in file listing operations
  3. When you need to display or record file names
  4. 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:

  1. First, get the complete path string
  2. Then find the last path separator ('' on Windows, '/' on Unix-like systems)
  3. 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

  1. If both the File If the object represents a root directory (such as "C:" or "/"),getName() will return an empty string ""
  2. The method does not check whether the file or directory actually exists
  3. The returned name includes the file extension (if any)
  4. For a relative path, only the last part of the path is returned

Comparison with Other Methods

getName() vs getPath()

getName() vs getAbsolutePath()


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 Java File