tags:

views:

37

answers:

1

Hello,

I need to extract all available parents for a file. For example, i have a file like "c:\users\admin\Downloads\1\2\3\4\5\test.stub", i need to add any parent to a list of Files, that it contains:

c:\users\admin\Downloads\1\2\3\4\5\
c:\users\admin\Downloads\1\2\3\4\
c:\users\admin\Downloads\1\2\3\
...
c:\

Any Ideas how i could make that?

Thanks in Advance,

Flo

+2  A: 

Try this:

File f = new File("c:\\users\\admin\\Downloads\\1\\2\\3\\4\\5\\");
while(getParent() != null){
   System.out.println(f.getPath());
   f = f.getParentFile();
}

getParent() returns the path of the parent directory (or null if there is no parent).
getParentFile() returns the parent directory as a File.


Resources :

Colin Hebert