What if i enter something valid like /home/pawal/hello.txt (let's suppose which actually exists).

if(file.exists()) returns true and which is not what we desired in order to check if it is a directory. 
It would be better to use file.isDirectory() alongside file.exists()to make sure user entered value resembles the directory structure.

I haven't tested it but it should work fine.

P.S Same comment for both of you.