java tutorial - How to Get parent directory of the file or directory in Java - java programming - learn java - java basics - java for beginners



 Get parent directory of the file or directory

Learn java - java tutorial - Get parent directory of the file or directory - java examples - java programs

Get parent directory of the file or directory:

  • The java.io.File.getParent() method returns the pathname is string or null and this path name is not a parent directory name.
  • In Java File getParent() method returns the path name string of the parent directory named of this abstract path name, or null and this pathname is not a parent name.
  • getParentFile(): Method returns the parent directory as a File object, or null and this File object is not a parent directory.
  • Following is the declaration for java.io.File.getParent() method

Syntax:

public String getParent()

    Sample Code:

    import java.io.File;
    public class kaashiv_infotech
    {
        public static void main(String[] args)
        {
            File myFile = new File("C:" + File.separator + "jdk" + File.separator, "kaashiv_file.java");
            System.out.println(myFile.getParent());
            System.out.println(myFile.getParentFile());
            System.out.println(myFile);
        }
    }
    

    Output:

    C:/jdk
    C:/jdk
    C:/jdk/kaashiv_file.java

Related Searches to How to Get parent directory of the file or directory in Java