因此,在对我的问题进行了多次编辑之后,我仍然无法解决这个问题。我有一个名为 ProcessList.txt 的 .txt 文件,每次在我的 Java 应用程序中执行 ps -e 命令时都会填充该文件。这是我用来将 ps -e 的输出重定向到 ProcessList.txt 的代码。
import java.io.*;
import java.util.StringTokenizer;
public class GetProcessList
{
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
// p = runTime.exec("tasklist"); // For Windows
p=runTime.exec("ps -e"); //For Linux
//Create Inputstream for Read Processes
InputStream inputStream = p.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//Read the processes from sysrtem and add & as delimeter for tokenize the output
String line = bufferedReader.readLine();
process = "&";
while (line != null) {
line = bufferedReader.readLine();
process += line + "&";
}
//Close the Streams
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
System.out.println("Processes are read.");
} catch (IOException e) {
System.out.println("Exception arise during the read Processes");
e.printStackTrace();
}
return process;
}
void showProcessData()
{
try {
//Call the method For Read the process
String proc = GetProcessListData();
//Create Streams for write processes
//Given the filepath which you need.Its store the file at where your java file.
OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(new FileOutputStream("ProcessList.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
//Tokenize the output for write the processes
StringTokenizer st = new StringTokenizer(proc, "&");
while (st.hasMoreTokens()) {
bufferedWriter.write(st.nextToken()); //Write the data in file
bufferedWriter.newLine(); //Allocate new line for next line
}
//Close the outputStreams
bufferedWriter.close();
outputStreamWriter.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
它在我的工作区中创建了一个名为 ProcessList.txt 的文件。它看起来像这样,有 4 列:
1 ? 00:00:00 init
2 ? 00:00:00 kthreadd
3 ? 00:00:00 ksoftirqd/0
5 ? 00:00:00 kworker/u:0
6 ? 00:00:00 migration/0
现在,创建 ProcessList.txt 文件后,我将此 .txt 文件的内容重定向到 JTable,如下所示:
import java.io.*;
import java.awt.*;
import java.util.*;import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
public class InsertFileToJtable extends AbstractTableModel{
Vector data;
Vector columns;
private String[] colNames = {"<html><b>PID</b></html>","<html><b>TTY</b></html>","<html><b>time</b></html>","<html><b>Process Name</b></html>",};
public InsertFileToJtable() {
String line;
data = new Vector();
columns = new Vector();
try {
FileInputStream fis = new FileInputStream("ProcessList.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");
while (st1.hasMoreTokens())
columns.addElement(st1.nextToken());
while ((line = br.readLine()) != null) {
StringTokenizer st2 = new StringTokenizer(line, " ");
while (st2.hasMoreTokens())
data.addElement(st2.nextToken());
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public int getRowCount() {
return data.size() / getColumnCount();
}
public int getColumnCount() {
return columns.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
return (String) data.elementAt((rowIndex * getColumnCount())
+ columnIndex);
}
@Override
public String getColumnName(int column) {
return colNames[column];
}
@Override
public Class getColumnClass(int col){
return getValueAt(0,col).getClass();
}
}
这是我的输出到目前为止的样子(如果我知道如何上传最终输出的屏幕截图 :( 但这里有一点关于输出 Jtable 到目前为止的样子。
=============================
PID TTY TIME PROCESS NAME
=============================
2 ? 00:00:00 kthreadd
3 ? 00:00:00 ksoftirqd/0
5 ? 00:00:00 kworker/u:0
6 ? 00:00:00 migration/0
请注意,ProcessList.txt 的内容被重定向到 JTAble,除了 ProcessList.txt 文件的第一行即
1 ? 00:00:00 init
似乎不在 JTable 输出中。任何帮助将不胜感激。我已经发布了很多关于此的问题,但没有运气:(
编辑:这是我的构造函数和 main()
public void InterfaceFrame(){
setTitle("My Frame");
add(tabbedPane);
Pack();
setVisible(true);
}
public static void main(String[] args) throws
URISyntaxException,
IOException,
InterruptedException {
panel.setSize(100,100);
panel.add(table);
model.fireTableStructureChanged();
table.setModel(model);
InsertFileToJtable model = new InsertFileToJtable();
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
RowSorter<TableModel> sorter =
new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
JScrollPane scrollpane = new JScrollPane(table);
panel.add(scrollpane, BorderLayout.CENTER);
JButton button = new JButton("Show View");
panel.add( button, BorderLayout.SOUTH );
tabbedPane.addTab("Process",null,scrollpane,"");
//////////////SOME OTHER TABS///////////////////////////
}