【发布时间】:2021-05-06 21:48:56
【问题描述】:
我通过 Java 进程运行命令以从 powershell 中获取多个磁盘卷。输出如下所示:
我现在想保存每个磁盘的一个实例,以便将它们插入到 SQL 数据库中。
这是我目前所处的位置:
public class Disk {
private String Letter;
private String Label;
private String Type;
private String Health;
private String Op;
private String Size;
private String Remaining;
public Disk(String letter, String label, String type, String health, String op, String size, String remaining) {
Letter = letter;
Label = label;
Type = type;
Health = health;
Op = op;
Size = size;
Remaining = remaining;
}
private List<Disk> diskTable = new ArrayList<Disk>();
public void getDiskInfo() {
//call the powershell process
ProcessBuilder pb = new ProcessBuilder();
pb.command("powershell.exe", "/c", "Get-Volume | fl DriveLetter, FileSystemLabel, FileSystemType, HealthStatus, OperationalStatus, Size, SizeRemaining");
try {
//read in the output from the powershell process
Process Diskprocess = pb.start();
BufferedReader Diskreader =
new BufferedReader(new InputStreamReader(Diskprocess.getInputStream()));
String line;
while ((line = Diskreader.readLine()) != null) {
//split the key and the value up as I won't need to store the key.
final String[] pieces = line.split(":", 2);
if (pieces.length > 1) {
String key = pieces[0];
String value = pieces[1];
//line below is just to check format of output
System.out.println(line);
//store each value into a disk instance
//add each value into the list of disks
}
}
} catch (Exception e) {
e.printStackTrace();
e.getCause();
}
}
//some other method here about adding into an SQL database
}
如果只有一个磁盘就好了,我可以将每个值输入到数据库中,但理论上一台计算机上可以有许多卷。
目前我并不太担心 SQL 部分。我相信如果我有一个磁盘阵列,我可以根据阵列的索引将每个磁盘插入到我的数据库中。
【问题讨论】: