java - Check files exist in a directory -
i want read files in folder using java. unfortunately files missing , npe.
public static hashmap<string, integer> getcputemp() throws ioexception { file directory = new file("/sys/devices/virtual/thermal"); if (directory.exists()) { hashmap<string, integer> usagedata = new hashmap<>(); file[] flist = directory.listfiles(); (file file : flist) { if (file.isdirectory() && file.getname().startswith("thermal_zone")) { ...................... } } return usagedata; } return null;
how can prevent , return null
if files not there? can show me solution java 8?
i npe here (file file : flist)
it should flist
null.
so, right after flist
creation, check null.
file[] flist = directory.listfiles(); if (flist == null) { return null; }
Comments
Post a Comment