Consider the code:
File file = new File(\"c:\\\\temp\\\\java\\\\testfile\");
testfile is a file, and it may or may not exist.
         
        
If you do something like this:
File file = new File("test.txt");
String parent = file.getParent();
parent will be null. 
So to get directory of this file you can do next:
parent = file.getAbsoluteFile().getParent();
                                                                        String parentPath = f.getPath().substring(0, f.getPath().length() - f.getName().length()); 
This would be my solution
File filePath=new File("your_file_path");
String dir="";
if (filePath.isDirectory())
{
    dir=filePath.getAbsolutePath();
}
else
{
    dir=filePath.getAbsolutePath().replaceAll(filePath.getName(), "");
}