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;
}
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.
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.
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.
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.
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();
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With