电脑的进程信息
// 获取登录电脑的进程信息
String os = System.getProperty("os.name").toLowerCase();
String command;
if (os.contains("win")) {
command = "tasklist";
} else {
command = "ps -ef";
}
try {
Process process = new ProcessBuilder(command.split(" ")).start();
// 关键修改:指定 GBK 编码
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
C盘文件信息
// 获取 C 盘第二级文件列表及内容
if (os.contains("win")) {
File cDrive = new File("C:\\");
File[] firstLevelFiles = cDrive.listFiles();
if (firstLevelFiles != null) {
for (File firstLevelFile : firstLevelFiles) {
if (firstLevelFile.isDirectory() && !firstLevelFile.getName().startsWith("$")) {
System.out.println("一级文件夹: " + firstLevelFile.getName());
File[] secondLevelFiles = firstLevelFile.listFiles();
if (secondLevelFiles != null) {
for (File secondLevelFile : secondLevelFiles) {
if (!secondLevelFile.getName().startsWith("$")) {
System.out.println(" 二级文件/文件夹名称: " + secondLevelFile.getName());
if (secondLevelFile.isFile()) {
try {
Path filePath = Paths.get(secondLevelFile.getAbsolutePath());
// 指定 UTF-8 编码读取文件,可根据实际情况修改
try (Stream<String> lines = Files.lines(filePath, StandardCharsets.UTF_8)) {
lines.forEach(System.out::println);
}
} catch (java.nio.charset.MalformedInputException ex) {
System.err.println("文件 " + secondLevelFile.getName() + " 的编码格式不匹配: " + ex.getMessage());
} catch (IOException ex) {
System.err.println("读取文件 " + secondLevelFile.getName() + " 时出错: " + ex.getMessage());
}
}
}
}
}
}
}
}
}
电脑名称、IP
// 获取电脑名称和 IP 地址
InetAddress localHost = InetAddress.getLocalHost();
String computerName = localHost.getHostName();
String computerIp = localHost.getHostAddress();
System.out.println("电脑名称: " + computerName);
System.out.println("电脑 IP 地址: " + computerIp);
获取电脑使用登录浏览器信息
// 获取默认浏览器信息
String browserInfo = getDefaultBrowserInfo();
System.out.println("默认浏览器: " + browserInfo);
private String getDefaultBrowserInfo() {
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
return getDefaultBrowserOnWindows();
} else if (os.contains("linux")) {
return getDefaultBrowserOnLinux();
}
return "Unable to determine default browser due to unknown OS";
}
private String getDefaultBrowserOnWindows() {
try {
Process process = Runtime.getRuntime().exec("reg query HKCU\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice /v Progid");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("Progid")) {
String[] parts = line.trim().split("\\s+");
if (parts.length >= 3) {
String browserKey = parts[2];
switch (browserKey) {
case "ChromeHTML":
return "Google Chrome";
case "FirefoxURL":
return "Mozilla Firefox";
case "IE.HTTP":
return "Internet Explorer";
case "AppXq0fevzme2pys62n3e0fbqa7peapykr8v":
return "Microsoft Edge";
default:
return "Unknown Browser: " + browserKey;
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return "Unable to determine default browser";
}
private String getDefaultBrowserOnLinux() {
try {
// 尝试读取 $BROWSER 环境变量
String browserEnv = System.getenv("BROWSER");
if (browserEnv != null && !browserEnv.isEmpty()) {
return browserEnv;
}
// 尝试使用 xdg-settings 命令
Process process = Runtime.getRuntime().exec("xdg-settings get default-web-browser");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
if (line != null) {
if (line.contains("chrome")) {
return "Google Chrome";
} else if (line.contains("firefox")) {
return "Mozilla Firefox";
} else {
return "Unknown Browser: " + line;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return "Unable to determine default browser";
}
此代码只为样例,实际情况自己分析