Java怎么获取电脑的风扇的转速和电脑CUP的温度是多少度?怎么区分华氏摄氏温度?
可以使用第三方库Open Hardware Monitor
获取计算机硬件信息,
【以下回答由 GPT 生成】
对于获取电脑的风扇转速和CPU温度,我有以下解决方案:
配置Java Native Access (JNA)库: JNA库允许我们使用Java调用底层系统功能。你可以从https://github.com/java-native-access/jna下载并导入JNA库。
使用JNA调用操作系统的相关功能:
import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult;
import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.W32Errors;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.platform.win32.WinReg;
public class FanCpuInfo {
private static final int HKLM_REGISTRY_ACCESS_RIGHTS = WinNT.KEY_READ;
public static void main(String[] args) {
String fanRPMPattern = "%\\Device%\\HWMONITOR\\";
String cpuTemperaturePattern = "%\\_TZ.TZ*\\_TMP.";
String fanRPMKey = findRegistryKey(fanRPMPattern);
String cpuTemperatureKey = findRegistryKey(cpuTemperaturePattern);
if (fanRPMKey == null || cpuTemperatureKey == null) {
System.out.println("Failed to find fan RPM key or CPU temperature key.");
return;
}
String fanRPMValue = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, fanRPMKey, "");
String cpuTemperatureValue = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, cpuTemperatureKey, "");
System.out.println("Fan RPM: " + fanRPMValue);
System.out.println("CPU Temperature: " + cpuTemperatureValue);
}
private static String findRegistryKey(String pattern) {
WinReg.HKEYByReference hKey = new WinReg.HKEYByReference();
int result = Advapi32Util.RegOpenKeyEx(WinReg.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Enum", 0, HKLM_REGISTRY_ACCESS_RIGHTS, hKey);
if (result != W32Errors.ERROR_SUCCESS) {
System.out.println("Failed to open registry key.");
return null;
}
String[] keyNames = Advapi32Util.registryGetKeys(hKey.getValue());
for (String keyName : keyNames) {
if (keyName.contains(pattern)) {
return "SYSTEM\\CurrentControlSet\\Enum\\" + keyName;
}
}
System.out.println("Registry key not found for pattern: " + pattern);
return null;
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FanCpuInfo {
private static final String FAN_RPM_PATH = "/sys/class/hwmon/hwmon0/fan1_input";
private static final String CPU_TEMPERATURE_PATH = "/sys/class/hwmon/hwmon0/temp1_input";
private static final int MILLIS_PER_SECOND = 1000;
public static void main(String[] args) {
try {
int fanRPM = Integer.parseInt(readFile(FAN_RPM_PATH));
int cpuTemperature = Integer.parseInt(readFile(CPU_TEMPERATURE_PATH));
System.out.println("Fan RPM: " + fanRPM);
System.out.println("CPU Temperature: " + (cpuTemperature / 1000.0));
} catch (IOException e) {
e.printStackTrace();
}
}
private static String readFile(String filePath) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
return reader.readLine();
}
}
}
综上所述,以上是我给出的解决方案。希望能对你有所帮助。如果你有任何疑问,请随时提问。
【相关推荐】