Java,546字节(Windows XP或更高版本)
powercfg实用程序已在WindowsXP中发挥作用,我不知道在此之前可以使用哪些实用程序。我还必须以管理员身份运行。这真是令人敬畏,我没有做过极端的高尔夫尝试...
import java.io.*;import java.util.Scanner;public class J {public static void main(String[] args)throws Exception{Process p=Runtime.getRuntime().exec("powercfg energy");p.waitFor();Scanner s=new Scanner(new File("C:\\windows\\system32\\energy-report.html"));String x;double a=0,b=0;while(s.hasNextLine()){x=s.nextLine();if(x.contains("Design Capacity")){s.nextLine();b=Integer.parseInt(s.nextLine().replaceAll("\\D+",""));}else if(x.contains("Last Full Charge")){s.nextLine();a=Integer.parseInt(s.nextLine().replaceAll("\\D+",""));}}System.out.print(a/b*100);}}
格式化/已注释...
import java.io.*;
import java.util.Scanner;
public class J {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec("powercfg energy"); // Run CMD sys32 app.
p.waitFor(); // Wait for it.
Scanner s = new Scanner(new File(
"C:\\windows\\system32\\energy-report.html")); // Read output.
String x;
double a = 0, b = 0;
while (s.hasNextLine()) {
x = s.nextLine();
if (x.contains("Design Capacity")) { // Find max capacity.
s.nextLine();
b = Integer.parseInt(s.nextLine().replaceAll("\\D+", ""));
} else if (x.contains("Last Full Charge")) { // Find current capacity.
s.nextLine();
a = Integer.parseInt(s.nextLine().replaceAll("\\D+", ""));
}
}
System.out.print(a / b * 100); // Calculate %.
}
}
老实说,我更感兴趣的是看看Java是否有可能。