0

I have a File pointing to a base dir:

File baseDir = new File("c:\\baseDir");

now I want to "go into a subdir":

File subDir = new File("c:\\baseDir\\subDir");

I only found how I can do it via strings concatenation. But is there an API function to do the job?

Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129
  • 1
    You might want to refer http://stackoverflow.com/questions/5125242/java-list-only-subdirectories-from-a-directory-not-files – Rudrani Angira Jun 26 '16 at 08:13

3 Answers3

2

There is one of the overloaded constructors serving the purpose:

> File(File parent, String child)

Creates a new File instance from a parent abstract pathname and a child pathname string.
Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129
0

This might help, getAbsolutePath() return the absolute path of the sub directory, no need to append the parent path.

String filePath = "/home/untitled";

    File[] directories = new File(filePath).listFiles(new FileFilter() {
        @Override
        public boolean accept(File file) {
            return file.isDirectory();
        }
    });

    for (File file : directories) {
        System.out.println(file.getAbsolutePath());
    }
shakhawat
  • 2,639
  • 1
  • 20
  • 36
0

You can use:

     File subDir = new File("c:\\baseDir","subDir");
c0der
  • 18,467
  • 6
  • 33
  • 65