gpt4 book ai didi

java - JTable从第二行读取文本文件

转载 作者:行者123 更新时间:2023-12-02 07:25:24 25 4
gpt4 key购买 nike

我正在开发从 .txt 文件读取文本的 JTable。 txt 文件会在 3 秒后动态更新。现在,当我运行该应用程序时,一切都很好,除了 .txt 文件的输出来自第二行的 JTable 中。 txt 文件的第一行没有出现在我的 JTable 上。有人可以帮忙吗?代码如下:

public class InterfaceFrame extends JFrame implements ActionListener{

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,"");
}

我在制作文本文件时可能做错了什么。这是生成 .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("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 并将输出提供给 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();
}
}

最佳答案

我的做法略有不同,避免使用中间文本文件。相反,

  • 使用ProcessBuilder ,“可以从同一实例重复调用”。您可以读取如下所示的输出 here并将其解析为合适的数据结构,例如List<List<String>> .

    ProcessBuilder pb = new ProcessBuilder("ps -ef");
  • 启动 javax.swing.Timer有三秒钟的时间;调用pb.start()在计时器的监听器中。

  • 解析结束时,fireTableDataChanged()在你的AbstractTableModel ,如图 here .

很快,您的表每三秒就会更新一次最新结果。

关于java - JTable从第二行读取文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13643907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com