io - Alternative to java.nio.file.Path in Java6 for OS File system Flexibility -
i backporting code java se6 , looking have os agnostic file retrieval code. have code on se7 works great.
this way using on java se7.
protected properties getpropertiesfromfilesystempath(final string filename) throws ioexception { if (filename != null) { path p = filesystems.getdefault().getpath(""); final inputstream inputstream = new fileinputstream(p.resolve(filename).tofile()); return getproperties(inputstream); } else { throw new ioexception(); } } with code can point file foo\bar\file.txt or foo/bar/file.txt , found.
is there alternative way easy using java.nio.file.path in java se6?
if receiving aways single file, using path concat function overkill.
you can use new fileinputstream(filename). work correctly on both / , \, if mix them up.
and important close input stream opened. in java7 can use autocloseable function:
try (inputstream = new fileinputstream(filename)) { return getproperties(is); } in java6 need close yourself:
inputstream = new fileinputstream(filename); try { return getproperties(is); } { if (is != null) is.close(); }
Comments
Post a Comment