Java Files  «Prev  Next»


Lesson 6Listing File information
ObjectiveIs it a file or directory?

Is this a Java File or a Directory?

Understand how to determine if a File object is a file or a directory.

We have seen how to determine whether the File object exists and how to get the object's name and path.
But we do not know if the object is a file or a directory.

isFile( ) method

The isFile() method indicates whether this file exists and whether it is a normal file, in other words it asks the question,
Is it a directory or not?
public boolean isFile()

The isDirectory( ) method


The isDirectory() method returns true if this file exists and is a directory.
public boolean isDirectory()

Getting the parent


The getParent() method returns a String that contains the name of the single directory which contains this file in the hierarchy.
public String getParent()
This method does not return a full path all the way back up to the root. If the file is at the top level of the disk, then it has no parent directory and null is returned.