File getParent() in Java

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

Syntax:

String java.io.File.getParent()

This method returns the pathname string 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 pathname string 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 FilegetParent {
    public static void main(String[] args) {

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

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

Output:

D:\


No comments:

Post a Comment