我是新來的Java。我想創建Java代碼獲取值到這些文件夾:所有文件不打印
/sys/devices/virtual/thermal/thermal_zone0/temp
/sys/devices/virtual/thermal/thermal_zone1/temp
這是一種不正常工作的代碼:
public static HashMap<String, HashMap<String, Double>> getTemp() throws IOException
{
HashMap<String, HashMap<String, Double>> usageData = new HashMap<>();
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)
{
if (file1.isFile() && file1.getName().startsWith("temp"))
{
byte[] fileBytes = null;
if (file1.exists())
{
try
{
fileBytes = Files.readAllBytes(file1.toPath());
}
catch (AccessDeniedException e)
{
}
if (fileBytes.length > 0)
{
HashMap<String, Double> usageData2 = new HashMap<>();
String number = file1.getName().replaceAll("^[a-zA-Z]*", "");
usageData2.put(number, Double.parseDouble(new String(fileBytes)));
usageData.put("data", usageData2);
}
}
}
}
}
}
return usageData;
}
最終的結果是這樣的:
{data={=80000.0}}
我發現的第一個問題是,當我使用整數來存儲該值時,出現錯誤。 第二個問題是我只有一個值。輸出應該是這樣的:
{data={0=80000.0}}
{data={1=80000.0}}
你能幫我找到問題嗎?
您不能使用同一個鍵將兩個值插入* normal *映射。這個'usageData.put(「data」,usageData2);'替換之前用關鍵字''data''存儲的值。您可以改用[Multimap](https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multimap)。 – Tom 2015-03-03 12:34:27
嘗試從'HashMap'切換到'ArrayList'。 – 2015-03-03 12:35:05
關於第二個問題?你知道爲什麼當我嘗試使用usageData2.put(number,Integer.parseInt(new String(fileBytes)));' – user1285928 2015-03-03 12:38:45