How to identify whether a given path is either file or directory in Java without using package?

Arjun :

I'm creating a Java Program that identifies whether the given path is either file or directory without using any package.
What are the attributes that distinguishes between a file and a directory ?
Also, how does isFile() and isDirectory() method from java.io.File works at backend?

I tried separating from the extension, but found that directory name also can contain characters after period like directoryName.extension. I've some research on internet but failed to come with solid solution.

class FileCheck{
    public static void main(String[] args){
        boolean checkOne = isFile(C:\users);
        boolean checkTwo = isFile(C:\users\file.txt);
        boolean checkThree = isDirectory(C:\users);
        boolean checkFour = isDirectory(C:\users\file.txt);
    }
}

Don't get confused with isFile() and isDirectory() method. They are user created functions; not imported from java.io.File.

I expect if I pass C:\users as argument, it should return true value from isDirectory() function and when C:\users\file.txt, it should return false. Same logic applies for isFile() method.

Karol Dowbecki :

What is a file and what is a directory ultimately depends on the file system. To distinguish between file and directory JVM will call the file system and get back the information. Knowing just the Path is not enough.

If you want to dig deeper it gets more complex. For example internally in Linux Ext4 filesystem everything is a file. Directory is just a special file with more information, as per Ext4 Disk Layout:

In an ext4 filesystem, a directory is more or less a flat file that maps an arbitrary byte string (usually ASCII) to an inode number on the filesystem. There can be many directory entries across the filesystem that reference the same inode number--these are known as hard links, and that is why hard links cannot reference files on other filesystems. As such, directory entries are found by reading the data block(s) associated with a directory file for the particular directory entry that is desired.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=92900&siteId=1