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:
- the relationship between 2 files documented code - they're siblings, sharing same filename prefix.
- it takes advantage of features of
path- if you're going direct string concatenations, might not usepath@ all. - it avoids
paths.get(), suboptimal way workpaths (because assumes default filesystem). once havepath, shouldn't ever need re-callpaths.get().
Comments
Post a Comment