Getting the Parents of a Filename Path - Java File Path IO

Java examples for File Path IO:Path

Description

Getting the Parents of a Filename Path

Demo Code

import java.io.File;

public class Main {

  public void main(String[] argv) {
    // Get the parent of a relative filename path
    File file = new File("Ex1.java");
    String parentPath = file.getParent(); // null
    File parentDir = file.getParentFile(); // null

    // Get the parents of an absolute filename path
    file = new File("D:\\folder\\Ex1.java");
    parentPath = file.getParent(); // D:\folder
    parentDir = file.getParentFile(); //  www. j a  v  a  2 s  .  com

    parentPath = parentDir.getParent(); // D:\
    parentDir = parentDir.getParentFile(); // D:\

    parentPath = parentDir.getParent(); // null
    parentDir = parentDir.getParentFile(); // null
  }
}

Related Tutorials