[英]Read values from files in different folders
I would like to read the values into these folders: 我想将值读入以下文件夹:
/sys/devices/virtual/thermal/thermal_zone0/temp:68000
/sys/devices/virtual/thermal/thermal_zone3/temp:50000
/sys/devices/virtual/thermal/thermal_zone5/temp:24900
/sys/devices/virtual/thermal/thermal_zone7/temp:62000
/sys/devices/virtual/thermal/thermal_zone8/temp:65000
/sys/devices/virtual/thermal/thermal_zone9/temp:78000
I tested this code: 我测试了这段代码:
public void listFolders() throws IOException
{
File directory = new File("/sys/devices/virtual/thermal");
File[] fList = directory.listFiles();
for (File file : fList)
{
if (file.isDirectory() && file.getName().startsWith("thermal_zone"))
{
File[] listFiles = file.listFiles();
for (File file1 : listFiles)
{
byte[] fileBytes = null;
if (file1.exists())
{
try
{
fileBytes = Files.readAllBytes(file1.toPath());
}
catch (AccessDeniedException e)
{
}
if (fileBytes.length > 0)
{
System.out.println(">>>>> " + fileBytes);
}
}
}
}
}
}
But I get null result when I test the code. 但是,当我测试代码时,结果为空。
Can you help me to fix the code? 您能帮我修复代码吗?
Also can you help me to optimize the code for performance? 您还能帮我优化代码以提高性能吗?
Instead of 代替
if (file1.isFile() && file.getName().startsWith("temp"))
Correct it to 更正为
if (file1.isFile() && file1.getName().startsWith("temp"))
Now you will see bytes printed. 现在,您将看到打印的字节。
In this statement, 在此声明中,
if (file1.isFile() && file.getName().startsWith("temp"))
after && use file1.getName()
&&使用
file1.getName()
Should work!!! 应该管用!!!
Also, it will print byte object's hashcode, not bytes, for that you need to iterate through array and print. 同样,它将打印字节对象的哈希码,而不是字节,因为您需要遍历数组并打印。 Look here for refernce
在这里寻找参考
Eg : System.out.println(Arrays.toString(fileBytes))
例如:
System.out.println(Arrays.toString(fileBytes))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.