Koerr
Koerr

Reputation: 15733

How do I get a file's directory using the File object?

Consider the code:

File file = new File("c:\\temp\\java\\testfile");

testfile is a file, and it may or may not exist. I want to get the directory c:\\temp\\java\\ using the File object. How do I go about doing this?

Upvotes: 124

Views: 227071

Answers (10)

Laike Endaril
Laike Endaril

Reputation: 1076

6/10/2021 All current answers fail if eg. the given file is...

new File("." + File.separator + "."); //Odd, yes, but I've seen similar more often than you'd think is possible

For a simple, robust solution, try...

File givenFile = new File("." + File.separator + ".." + File.separator + "." + File.separator + "fakeFilename"); //The file or dir we want the parent of, whether it exists or not
File parentFile = new File(givenFile.getAbsolutePath() + File.separator + "..");
System.out.println(parentFile.getAbsolutePath());

Note that this answer assumes you want the actual parent directory on the filesystem, and not a File object representing the parent node of the given File object (which may be null, or may be the same directory as the given file).

EDIT: Also note that the internal filename is not succinct, and could in some cases grow with repeated usage. An equivalent solution without that issue would need to parse the entire absolute filename given by the above solution, and adjust the output, removing all instances of "." and ".." (the latter ofc would need to also remove the node before it, but only after all "." are removed)

Upvotes: 0

Joel
Joel

Reputation: 30166

File.getParent() from Java Documentation

Upvotes: 28

nobes
nobes

Reputation: 21

String parentPath = f.getPath().substring(0, f.getPath().length() - f.getName().length()); 

This would be my solution

Upvotes: 2

Ponaguynik
Ponaguynik

Reputation: 970

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();

Upvotes: 26

C.Ikongo
C.Ikongo

Reputation: 1856

I found this more useful for getting the absolute file location.

File file = new File("\\TestHello\\test.txt");
System.out.println(file.getAbsoluteFile());

Upvotes: -1

Saurabh
Saurabh

Reputation: 7843

You can use this

 File dir=new File(TestMain.class.getClassLoader().getResource("filename").getPath());

Upvotes: -1

Jigar Joshi
Jigar Joshi

Reputation: 240996

File directory = new File("Enter any 
                directory name or file name");
boolean isDirectory = directory.isDirectory();
if (isDirectory) {
  // It returns true if directory is a directory.
  System.out.println("the name you have entered 
         is a directory  : "  +    directory);  
  //It returns the absolutepath of a directory.
  System.out.println("the path is "  + 
              directory.getAbsolutePath());
} else {
  // It returns false if directory is a file.
  System.out.println("the name you have
   entered is a file  : " +   directory);
  //It returns the absolute path of a file.
  System.out.println("the path is "  +  
            file.getParent());
}

Upvotes: 3

Lovenish Goyal
Lovenish Goyal

Reputation: 72

File filePath=new File("your_file_path");
String dir="";
if (filePath.isDirectory())
{
    dir=filePath.getAbsolutePath();
}
else
{
    dir=filePath.getAbsolutePath().replaceAll(filePath.getName(), "");
}

Upvotes: 3

YoK
YoK

Reputation: 14505

File API File.getParent or File.getParentFile should return you Directory of file.

Your code should be like :

    File file = new File("c:\\temp\\java\\testfile");
    if(!file.exists()){
        file = file.getParentFile();
    }

You can additionally check your parent file is directory using File.isDirectory API

if(file.isDirectory()){
    System.out.println("file is directory ");
}

Upvotes: 9

Jon Skeet
Jon Skeet

Reputation: 1503749

In either case, I'd expect file.getParent() (or file.getParentFile()) to give you what you want.

Additionally, if you want to find out whether the original File does exist and is a directory, then exists() and isDirectory() are what you're after.

Upvotes: 197

Related Questions