Java Directory Create mkdirs(File f)

Here you can find the source of mkdirs(File f)

Description

Like File.mkdirs() this creates all parent directories of f, but returns them in a list

License

Open Source License

Declaration

public static List<File> mkdirs(File f) 

Method Source Code

//package com.java2s;
/*//from   w  w w.  j a v  a 2 s . c  om
Copyright 2009 Hauke Rehfeld
    
    
This file is part of QuakeInjector.
    
QuakeInjector is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
QuakeInjector is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with QuakeInjector.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.File;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Like File.mkdirs() this creates all parent directories of f, but returns them in a list
     */
    public static List<File> mkdirs(File f) {
        ArrayList<File> files = new ArrayList<File>();

        if (f.isDirectory()) {
            files.add(f);
        }

        File parentDir = f.getParentFile();
        while (parentDir != null && !parentDir.exists()) {
            files.add(parentDir);
            parentDir = parentDir.getParentFile();
        }

        java.util.Collections.reverse(files);

        for (File dir : files) {
            System.out.println("Creating dir " + dir);
            dir.mkdir();
        }

        return files;
    }
}

Related

  1. mkdirs(File dumpFile)
  2. mkdirs(File f)
  3. mkDirs(File f)
  4. mkdirs(File f)
  5. mkdirs(File f)
  6. mkdirs(File file)
  7. mkdirs(File file)
  8. mkdirs(File file)
  9. mkdirs(File file)