2012-12-02 203 views
0

我想将命令“ps -e> /home/workspace/MyProject/List.txt”的输出重定向到JTable。有没有办法做到这一点?如果是,如何?从.txt文件读取数据并将其存储在JTable中

PS:我希望这个命令可以连续执行,直到用户退出并且JTable每次执行该命令时都会更新。

runtime.exec("ps -e > /home/workspace/MyProject/List.txt"); 

我已经使用watchservice API来监视MyProject目录中的更改。

所以经过如此多的编辑之后,我开始意识到命令没有用run.exec()执行。当我从终端执行它时,它运行良好,但是当我在Java程序中运行它时,被建造。任何人都可以告诉我我做错了什么?我使用的是Ubuntu 12.04。

+1

*“有什么办法吗?”*是的,有。我应该把它作为答案吗? [你有什么尝试?](http://www.whathaveyoutried.com/) –

+0

@AndrewThompson是的请。我会标记它,如果它适用于我:( –

+0

为什么你应该把它写在一个文件中,而不是你可以直接在表中 – vels4j

回答

1

用windows Tasklist命令测试,你用linux命令试试。

public class ProcessListTable { 

    private String GetProcessListData() { 
    Process p; 
    Runtime runTime; 
    String process = null; 
    try { 
     System.out.println("Processes Reading is started..."); 

     //Get Runtime environment of System 
     runTime = Runtime.getRuntime(); 

     //Execute command thru Runtime 
     // use appropriate command for linux "ps" 
     p = runTime.exec("tasklist /FO CSV /nh"); 

     //Create Inputstream for Read Processes 
     InputStream inputStream = p.getInputStream(); 
     InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
     BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 


     ArrayList<String> taskEntries = new ArrayList(); 
     String line = bufferedReader.readLine(); 
     while (line != null) { 
      taskEntries.add(line); 
      line = bufferedReader.readLine(); 
     } 
     bufferedReader.close(); 
     inputStreamReader.close(); 
     inputStream.close(); 

     MyTableModel myTableModel = new MyTableModel(); 
     myTableModel.update(taskEntries); 
     JTable table = new JTable(myTableModel); 
     JFrame frame = new JFrame("TaskList"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.add(new JScrollPane(table)); 
     frame.pack(); 
     frame.setVisible(true); 


     System.out.println("Processes are read."); 
    } catch (IOException e) { 
     System.out.println("Exception arise during the read Processes"); 
     e.printStackTrace(); 
    } 
    return process; 
} 

public class MyTableModel extends AbstractTableModel { 

    String[] columnName = new String[]{"Image Name", "PID", "Session Name", "Session#", "Mem Usage"}; 
    String[][] valueA; 

    public void update(ArrayList<String> taskEntries) { 
     valueA = new String[taskEntries.size()][columnName.length]; 
     int size = taskEntries.size(); 
     for (int i = 0; i < size; i++) { 
      String entry = taskEntries.get(i); 
      String[] splitValues = entry.split(","); 
      for (int j = 0; j < splitValues.length; j++) { 
       String v = splitValues[j]; 
       v = v.replaceAll("\"", ""); 
       // mem contains "," so added mem usage at the end 
       if (j >= 5) { 
        valueA[i][4] = valueA[i][4] + v; 
       } else { 
        valueA[i][j] = v; 
       } 
      } 
     } 
    } 

    @Override 
    public int getRowCount() { 
     return valueA.length; 
    } 

    @Override 
    public String getColumnName(int column) { 
     return columnName[column]; 
    } 

    @Override 
    public int getColumnCount() { 
     return columnName.length; 
    } 

    @Override 
    public Object getValueAt(int rowIndex, int columnIndex) { 
     return valueA[rowIndex][columnIndex]; 
    } 
} 

public static void main(String[] args) { 
    ProcessListTable gpl = new ProcessListTable(); 
    gpl.GetProcessListData(); 
} 
} 
+0

可能要看的东西。1)'ProcessBuilder'(让它更容易erge'out'和'err'流)2)同时使用两个流。 3)将命令分解为一个String [](不是用这个命令似乎是必要的,但是当使用带空格的路径时会混淆) - +1作为你的详细答案。 :) –

+1

@AndrewThompson谢谢。我只是大致反对这个评论*“这个问题显然过于宽泛,为什么不投票结束它,而不是给出一个正确的,但无益的答案?”* – vels4j

+0

好的电话。现在删除。 ;) –

相关问题