java - How do I force mkdir to overwrite existing directory? -
i need have programme create directory specific name, , overwrite existing directory name. currently, programme doesn't seem able overwrite directory. there way of forcing overwrite?
private boolean makedirectory(){ file file = new file(temp_dir_path + "/" + clipname); if (file.mkdir()) { homecoming true; } else { system.err.println("failed create directory!"); homecoming false; } }
edit: i'm trying following, programme not detecting directory exists, though 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()) { homecoming true; } else { system.err.println("failed create directory!"); homecoming false; } }
resolved: (if else in future needs know...) ended doing way:
private boolean makedirectory(string path){ if (files.exists(paths.get(path))) { seek { fileutils.deletedirectory(new file(path)); } grab (ioexception ex) { system.err.println("failed create directory!"); homecoming false; } } if (new file(path).mkdir()) { homecoming true; } homecoming false; }
mkdir
idempotent operation (it ensures directory exists checking if there, , if not, creating it), need delete directory first if exists, recreate it.
using java.nio.file.files
if (files.exists(path)) { new file("/dir/path").delete(); } new file("/dir/path").mkdir();
java
No comments:
Post a Comment