java - Return null when file is missing or empty -
i'm using code hdd data linux:
public void gethddinfo() throws ioexception { try (directorystream<path> ds = files.newdirectorystream(paths.get("/sys/block"), "sd*")) { // hdd model streamsupport.stream(ds.spliterator(), false) .map(p -> p.resolve("device/model")).flatmap(wrap(files::lines)) } try (directorystream<path> ds = files.newdirectorystream(paths.get("/sys/block"), "sd*")) { // hdd vendor streamsupport.stream(ds.spliterator(), false) .map(p -> p.resolve("device/vendor")).flatmap(wrap(files::lines)) } try (directorystream<path> ds = files.newdirectorystream(paths.get("/sys/block"), "sd*")) { // hdd state streamsupport.stream(ds.spliterator(), false) .map(p -> p.resolve("device/state")).flatmap(wrap(files::lines)) .foreach(system.out::println); } try (directorystream<path> ds = files.newdirectorystream(paths.get("/sys/block"), "sd*")) { // hdd revision streamsupport.stream(ds.spliterator(), false) .map(p -> p.resolve("device/rev")).flatmap(wrap(files::lines)) .foreach(system.out::println); } try (directorystream<path> ds = files.newdirectorystream(paths.get("/sys/block"), "sd*")) { // hdd scsi level streamsupport.stream(ds.spliterator(), false) .map(p -> p.resolve("device/scsi_level")).flatmap(wrap(files::lines)) .foreach(system.out::println); } try (directorystream<path> ds = files.newdirectorystream(paths.get("/sys/block"), "sd*")) { // hdd scsi removable streamsupport.stream(ds.spliterator(), false) .map(p -> p.resolve("device/removable")).flatmap(wrap(files::lines)) .foreach(system.out::println); } try (directorystream<path> ds = files.newdirectorystream(paths.get("/sys/block"), "sd*")) { // hdd scsi level streamsupport.stream(ds.spliterator(), false) .map(p -> p.resolve("device/size")).flatmap(wrap(files::lines)) .foreach(system.out::println); } }
but not files available or of them empty. exception:
java.nio.file.nosuchfileexception: \sys\block
i there way modify code return null when there no such file or file empty?
this should handle cases files don't exist:
try { // of stuff } catch (nosuchfileexception ex) { return null; }
Comments
Post a Comment