1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/* Get parent directory as a File object This Java example shows how to get the parent directory of a particular file or directory as File object using getParentFile method of Java File class. */ import java.io.*; public class GetParentDirectoryAsFile { public static void main(String[] args) { //create file object File file = new File("C://FileIO/demo.txt"); /* * To get parent directory of a particular file, use * String getParent() method of Java File class. */ File fileParent = file.getParentFile(); System.out.println("Parent directory is : " + fileParent.getPath()); } } /* Output would be Parent directory is : C:FileIO */ |
Add Comment