File getName() in Java

getName(): This method is available in the java.io.File class of Java.

Syntax:

String java.io.File.getName()

This method returns the name of the file or directory denoted by this abstract pathname.

Note: If the pathname's name sequence is empty, then the empty string is returned.

Parameters: NA

Returns: The name of the file or directory denoted by this abstract pathname, or the empty string if this pathname's name sequence is empty.

Exceptions: NA

Approach

Java

import java.io.File;

public class FilegetName {
    public static void main(String[] args) {

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);

        System.out.println(file.getName());
    }
}

Output:

hello.txt


No comments:

Post a Comment