java - Test if a file with a specific suffix exists using nio2 -


given: file named example.xml aim: test if file example.xml.sha256 exists.

what elegant/efficient way in java 7+ (nio2, using java.nio.files)?

i have this, looks little bit ugly me:

path path = paths.get("/../example.xml"); if (files.exists(paths.get(path.tostring() + ".sha256")))) {    ... } 

it'd little cleaner use .resolvesibling(), e.g.

path.resolvesibling(path.getfilename() + ".sha256"); 

this not dramatically better solution, have few advantages:

  1. the relationship between 2 files documented code - they're siblings, sharing same filename prefix.
  2. it takes advantage of features of path - if you're going direct string concatenations, might not use path @ all.
  3. it avoids paths.get(), suboptimal way work paths (because assumes default filesystem). once have path, shouldn't ever need re-call paths.get().

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -