Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force mkdir to overwrite existing directory?

Tags:

java

I need to have my program create a directory with a specific name, and overwrite any existing directory with that name. Currently, my program doesn't seem to be able to overwrite the directory. Is there any way of forcing the overwrite?

private boolean makeDirectory(){
    File file = new File(TEMP_DIR_PATH + "/" + clipName);
    if (file.mkdir()) {
        return true;
    }
    else {
        System.err.println("Failed to create directory!");
        return false;
    }
}

EDIT: Now I'm trying the following, but the program is not detecting that the directory exists, even though it does.

private boolean makeDirectory(String path){
    File file = new File(path);
    if (file.exists()) {
        System.out.println("exists");
        if (file.delete()) {
            System.out.println("deleted");
        }
    }
    if (file.mkdir()) {
        return true;
    }
    else {
        System.err.println("Failed to create directory!");
        return false;
    }
}

RESOLVED: (If anyone else in the future needs to know...) I ended up doing it this way:

private boolean makeDirectory(String path){
    if (Files.exists(Paths.get(path))) {
        try {
            FileUtils.deleteDirectory(new File(path));
        }
        catch (IOException ex) {
            System.err.println("Failed to create directory!");
            return false;
        }
    }    
    if (new File(path).mkdir()) {
        return true;
    }
    return false;
}
like image 403
labananala Avatar asked Oct 15 '14 21:10

labananala


People also ask

Does mkdir overwrite existing directories?

1 Answer. Show activity on this post. The mkdir command will create any folders that do not exist in the specified path, unless extensions are disabled ( setLocal enableExtensions ) - regardless, it will not destroy a directory and create a new one with the same name.

What happens if you mkdir a directory that already exists?

mkdir WILL give you an error if the directory already exists. mkdir -p WILL NOT give you an error if the directory already exists. Also, the directory will remain untouched i.e. the contents are preserved as they were.

How do I override a directory?

Usually, when you run a cp command, it overwrites the destination file(s) or directory as shown. To run cp in interactive mode so that it prompts you before overwriting an existing file or directory, use the -i flag as shown.

How do you mkdir only if a directory does not already exist?

You can either use an if statement to check if the directory exists or not. If it does not exits, then create the directory. You can directory use mkdir with -p option to create a directory. It will check if the directory is not available it will.


2 Answers

You want to delete the directory first if it exists, then recreate it.

Using java.nio.file.Files

if (Files.exists(path)) {
    new File("/dir/path").delete();
} 

new File("/dir/path").mkdir();

and if you have FileUtils, this might be preferable as it avoids actually deleting a directory you want to be there:

import org.apache.commons.io.FileUtils

if (Files.exists(path)) {
    FileUtils.cleanDirectory( new File("/dir/path"));
} else {
    new File("/dir/path").mkdir();
}
like image 91
Russia Must Remove Putin Avatar answered Nov 15 '22 12:11

Russia Must Remove Putin


You can import this library import org.apache.commons.io.FileUtils; and then you could write your code like this:

private boolean makeDirectory(){
    File file = new File(TEMP_DIR_PATH + "/" + clipName);
    boolean returnValue = false;
    try {
         FileUtils.forceMkdir(file);
         returnValue = true;
    } catch (IOException e) {
         throw new RuntimeException(e);
    }
    return returnValue;
}
like image 36
euthimis87 Avatar answered Nov 15 '22 10:11

euthimis87