java - Check if folder exists and if so add "New Folder 2" -



java - Check if folder exists and if so add "New Folder 2" -

i have web application inherited. new java don't beat me bad. have next method add together new folders attachment page. user can create new folders on page , rename, how check see if "new folder" exists , if create "new folder (2)" or "new folder (3)" etc...

here method attachments servlet:

protected void newfolderaction(httpservletrequest request, httpservletresponse response, user user, string folderid) throws unsupportedencodingexception, ioexception { string key = request.getparameter("key"); string value = request.getparameter("value"); attachment parent = attachmentrepository.read(uuid.fromstring(key)); string path = parent.getpath(); logger.debug("newfolder: key=" + key + " value=" + value + " path=" + path); if (attachmentrepository.read(path + "new folder/") == null) { long size = 0l; boolean isfolder = true; boolean ispicture = false; uuid attachmentid = uuid.randomuuid(); attachment attachment = new attachment(attachmentid, uuid.fromstring(folderid), user.getunitid(), uuid.fromstring("11111111-1111-1111-1111-111111111111"), path + "new folder/", size, isfolder, ispicture, "", "0", "0", user.getname(), new date()); attachmentrepository.add(attachment); file directory = new file(settings.instance().getattachmentsdir() + "/" + attachment.getpath()); directory.mkdirs(); } attachment rootattachment = attachmentrepository.read(folderid + "/"); writejsonattachmentstree(response, user, request.getrequesturi(), rootattachment); }

there no custom built-in function in java create directory if desired name exists, should implement 1 yourself.

public static void main(string[] args) { file folderpath = new file("c:\\new folder"); // check whatever folderpath exists system.out.println(folderpath.getpath() + " directory ? " + folderpath.isdirectory()); // create new folder file foldercreated = createfolder(folderpath); system.out.println("the new directory path is: " + foldercreated.getpath()); // check whatever folderpath exists system.out.println(foldercreated.getpath() + " directory ? " + foldercreated.isdirectory()); } public static file createfolder(file path) { file pathnum = new file(path.getpath()); string num = ""; int = 1; { pathnum = new file(path.getpath() + num); num = "(" + ++i + ")"; } while (!pathnum.mkdir()); homecoming pathnum; }

java ajax

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -