java - Check if file exists using Apache Commons VFS2 -


i ask if there way how check if file exists in folder using apache commons.

i have method uploads sftp folder overwrites current files anytime method running. method set run every 5 minutes. need code create , if statement checks if file not @ sftp location , then, if not executes copy method, if there file, skips it.

my copy method looks this

private void copyfilesftp(file model, string hour) throws ioexception {     standardfilesystemmanager manager = new standardfilesystemmanager();     string dest = string.format("%s/%s/model/%s", destinationpath, hour,             model.getname());      remotedirectory = string.format("%s/%s/model/", destinationpath, hour);      try {         if (!model.exists())             log.error("error. local file not found");          // initializes file manager         manager.init();          // setup our sftp configuration         filesystemoptions opts = new filesystemoptions();         sftpfilesystemconfigbuilder.getinstance().setstricthostkeychecking(                 opts, "no");         sftpfilesystemconfigbuilder.getinstance().setuserdirisroot(opts,                 false);         sftpfilesystemconfigbuilder.getinstance().settimeout(opts, 10000);          // create sftp uri using host name, userid, password, remote         // path , file name         string sftpuri = "sftp://" + userid + ":" + password + "@"                 + serveraddress + "/" + remotedirectory + model.getname();          **here need check if model exists on sftp**          // create local file object         fileobject localfile = manager.resolvefile(model.getabsolutepath());          // create remote file object         fileobject remotefile = manager.resolvefile(sftpuri, opts);          // copy local file sftp server         remotefile.copyfrom(localfile, selectors.select_self);         log.info("file upload successful");         log.info("new file has been created.");         log.info(dest);      } catch (exception ex) {         log.error(ex);         handlebadpath(model, hour);      } {         manager.close();     }  } 

thank help.

use fileobject.exists() method.

see https://commons.apache.org/proper/commons-vfs/apidocs/org/apache/commons/vfs2/fileobject.html#exists%28%29


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -