tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > IO > Parent Directory

Parent Directory 

The following example shows how to get parent directory of the given file. You can observe that the parent directory of root file system is null.

File Name  :  
com/bethecoder/tutorials/io/ParentDirectory.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.io;

import java.io.File;

public class ParentDirectory {

  /**
   @param args
   */
  public static void main(String[] args) {
    File directory = new File("C:\\NEW\\NEW_SUB");
    System.out.println("Directory : " + directory);
    
    System.out.println("Parent Directory : " 
        directory.getParentFile());
    System.out.println("Parent Parent Directory : " 
        directory.getParentFile().getParentFile());
    System.out.println("Parent Parent Parent Directory : " 
        directory.getParentFile().getParentFile().getParentFile());
    
  }

}
   

It gives the following output,
Directory : C:\NEW\NEW_SUB
Parent Directory : C:\NEW
Parent Parent Directory : C:\
Parent Parent Parent Directory : null



 
  


  
bl  br