File getParentFile() in Java

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

Syntax:

File java.io.File.getParentFile()

This method returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory.

Note: If the name sequence is empty then the pathname does not name a parent directory.

Parameters: NA

Returns: The abstract pathname of the parent directory named by this abstract pathname, or null if this path name does not name a parent.

Exceptions: NA

Approach

Java

import java.io.File;

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

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

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

Output:

D:\


No comments:

Post a Comment