【问题标题】:Storing each value of a PowerShell process into a Java object array将 PowerShell 进程的每个值存储到 Java 对象数组中
【发布时间】: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 部分。我相信如果我有一个磁盘阵列,我可以根据阵列的索引将每个磁盘插入到我的数据库中。

【问题讨论】:

    标签: java arrays oop object


    【解决方案1】:

    这些值是按照您指定的顺序排列的,因此每当您看到DriveLetter 的值时,您就知道新驱动器的信息将随之而来。使用它。

    diskTable.clear();
    Disk disk = null;
    
    for (String line; (line = Diskreader.readLine()) != null; ) {
        final String[] pieces = line.split(":", 2);
        if (pieces.length == 2) {
            String name = pieces[0].trim();
            String value = pieces[1].trim();
    
            switch (name) {
                case "DriveLetter":
                    disk = new Disk();
                    diskTable.add(disk);
                    disk.Letter = value;
                    break;
                case "FileSystemLabel":
                    disk.Label = value;
                    break;
                ...
                case "SizeRemaining":
                    disk.Remaining = value;
                    break;
                default:
                    // Ignore
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 2013-11-06
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多