String java.io.File.getParent()
Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.
The parent of an abstract pathname consists of the pathname's prefix, if any, and each name in the pathname's name sequence except for the last. If the name sequence is empty then the pathname does not name a parent directory.
Returns:
The pathname string of the parent directory named by this abstract pathname, or null if this pathname does not name a parent
ParentDirectory.java
Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.
The parent of an abstract pathname consists of the pathname's prefix, if any, and each name in the pathname's name sequence except for the last. If the name sequence is empty then the pathname does not name a parent directory.
Returns:
The pathname string of the parent directory named by this abstract pathname, or null if this pathname does not name a parent
ParentDirectory.java
package com.javatutorialcorner.directory; import java.io.File; public class ParentDirectory { public static void main(String[] args) { String filePath = "C:/JTC/Java/Examples/Directory/File/java.txt"; File file = new File(filePath); String parentDirectory = file.getParent(); System.out.println("Parent directory : " + parentDirectory); if (!file.isDirectory()) { System.out.println("This is File"); System.out.println("Parent directory of this file is :" + file.getParent()); } else { System.out.println("This is Directory"); System.out.println("Parent directory of this directory is :" + file.getParent()); } } }Output
Parent directory : C:\JTC\Java\Examples\Directory\File
This is File
Parent directory of this file is :C:\JTC\Java\Examples\Directory\File
0 comments:
Post a Comment