我有一个.txt文件,其中包含以下数据(注意:文件中的数据会定期更新,因此我不确切知道行数)。我的Java应用程序在JTable中显示此数据。这是我的txt文件:
root 1 0 0 09:49 ? 00:00:00 init [5]
root 2 0 0 09:49 ? 00:00:00 [kthreadd]
root 3 2 0 09:49 ? 00:00:00 [ksoftirqd/0]
root 4 2 0 09:49 ? 00:00:00 [kworker/0:0]
root 6 2 0 09:49 ? 00:00:00 [migration/0]
root 7 2 0 09:49 ? 00:00:00 [watchdog/0]
我编码的是:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
public class Reader {
public Reader(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream("ProcessList.txt")));
Map<String, Object[]> result = new LinkedHashMap<String, Object[]>();
while(reader.ready()){
String line = reader.readLine();
String[] values = line.split("\\s+");
String key = values [0] +"&"+values[4] +"&"+values[5]+"&"+values[6]+"&"+values[7];
String uid = values [0];
String strtime = values [4];
String tty = values [5];
String etime = values [6];
String command = values [7];
Integer pid = 0;
Integer pri = 0;
Integer uti = 0;
if (result.containsKey(key)) {
pid = (Integer) ((Object[]) result.get(key))[1];
pri = (Integer) ((Object[]) result.get(key))[2];
uti = (Integer) ((Object[]) result.get(key))[3];
}
else {}
result.put(key, new Object[]{uid,pid,pri,uti,strtime,tty,etime,command});
}
ArrayList arrayList = new ArrayList(result.values());
JTable table = new JTable(new PTablemodel(arrayList));
panel.add(new JScrollPane(table));
reader.close();
frame.setContentPane(panel);
frame.setVisible(true);
frame.pack();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
public static void main (String [] args){
new Reader();
}
public class PTablemodel implements TableModel{
final Class[] ColumnClass = new Class[]{String.class, Integer.class, Integer.class,Integer.class, String.class, String.class,String.class, String.class};
final String[] columnname = new String[]{"User","PID","Priority","Utilization","TTY","StartTime","EndTime","Command"};
List values = null;
public PTablemodel (List values){
this.values = values;
}
@Override
public void addTableModelListener(TableModelListener l) {
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return ColumnClass[columnIndex];
}
@Override
public int getColumnCount() {
return ColumnClass.length;
}
@Override
public String getColumnName(int columnIndex) {
return columnname[columnIndex];
}
@Override
public int getRowCount() {
return values.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return ((Object[]) values.get(rowIndex))[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public void removeTableModelListener(TableModelListener l) {
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}
}
}
我得到的并不是错误,而是捕获了异常。任何人都可以指导我吗?我用过“&amp;”作为标记器。
注意:这是编译器所说的内容:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Reader.<init>(Reader.java:30)
at Reader.main(Reader.java:65)
答案 0 :(得分:2)
此代码是基于您的SSCCE,它表明它是文件读取代码中的内容。
File - exists: true I:\projects\eclipse\Test\ProcessList.txt
Frame is visible
File - exists: false I:\projects\eclipse\Test\ProcessList1.txt
java.io.FileNotFoundException: ProcessList1.txt (The system cannot find the file specified)
...
package test.t100.t001;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
public class Reader {
static String name = "ProcessList";
public Reader(String name){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
BufferedReader reader;
try {
File f = new File(name);
System.out.println("File - exists: " + f.exists() + " " + f.getAbsolutePath());
reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(f)));
Map<String, Object[]> result = new LinkedHashMap<String, Object[]>();
while(reader.ready()){
String line = reader.readLine();
String[] values = line.split("\\s+");
String key = values [0] +"&"+values[4] +"&"+values[5]+"&"+values[6]+"&"+values[7];
String uid = values [0];
String strtime = values [4];
String tty = values [5];
String etime = values [6];
String command = values [7];
Integer pid = 0;
Integer pri = 0;
Integer uti = 0;
if (result.containsKey(key)) {
pid = (Integer) ((Object[]) result.get(key))[1];
pri = (Integer) ((Object[]) result.get(key))[2];
uti = (Integer) ((Object[]) result.get(key))[3];
}
else {}
result.put(key, new Object[]{uid,pid,pri,uti,strtime,tty,etime,command});
}
ArrayList arrayList = new ArrayList(result.values());
JTable table = new JTable(new PTablemodel(arrayList));
panel.add(new JScrollPane(table));
reader.close();
frame.setContentPane(panel);
frame.setVisible(true);
frame.pack();
System.out.println("Frame is visible");
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
public static void main (String [] args) throws IOException{
String s =
"root 1 0 0 09:49 ? 00:00:00 init [5]\n" +
"root 2 0 0 09:49 ? 00:00:00 [kthreadd]\n" +
"root 3 2 0 09:49 ? 00:00:00 [ksoftirqd/0]\n" +
"root 4 2 0 09:49 ? 00:00:00 [kworker/0:0]\n" +
"root 6 2 0 09:49 ? 00:00:00 [migration/0]\n" +
"root 7 2 0 09:49 ? 00:00:00 [watchdog/0]\n";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileOutputStream fos = new FileOutputStream(name + ".txt");
fos.write(s.getBytes());
new Reader(name + ".txt");
new Reader(name + "1.txt");
}
public class PTablemodel implements TableModel{
final Class[] ColumnClass = new Class[]{String.class, Integer.class, Integer.class,Integer.class, String.class, String.class,String.class, String.class};
final String[] columnname = new String[]{"User","PID","Priority","Utilization","TTY","StartTime","EndTime","Command"};
List values = null;
public PTablemodel (List values){
this.values = values;
}
@Override
public void addTableModelListener(TableModelListener l) {
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return ColumnClass[columnIndex];
}
@Override
public int getColumnCount() {
return ColumnClass.length;
}
@Override
public String getColumnName(int columnIndex) {
return columnname[columnIndex];
}
@Override
public int getRowCount() {
return values.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return ((Object[]) values.get(rowIndex))[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public void removeTableModelListener(TableModelListener l) {
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}
}
}
答案 1 :(得分:2)
您NullPointerException
中的while-loop
读取了输入。您应该测试reader.readLine()
不返回null
。如果是,则表示您已到达输入的末尾。
你可以这样做:
while((String line = reader.readLine())!=null) {
...
}
答案 2 :(得分:0)
如果您收到FileNotFoundException- 确保将ProcessList.txt文件保留在项目根目录中。
如果要从命令提示符编译并运行它们,请确保ProcessList.txt文件位于.class文件所在的位置。
答案 3 :(得分:0)
此页面似乎一直在变化。按照我写的那样看,你在第30行有一个糟糕的数组索引。具体来说,你试图得到数组value
的元素4,奇怪的是,它没有一个。我不是一个正则表达式专家,但可能是你在line.split("\\s+")
中得到了额外的反斜杠。如果这是正确的,那么您正在读取的数据有问题。您可能首先打印line
(或在调试器中检查)或仅处理实际情况。它可能只是输入中有一个或多个空行或空行,当你查看文件时你看不到,但遗憾的是readLine()
。
(我经常写自己的readLine
所以我可以删除不可打印的字符和多行换行等等 - 所以我不是在写这个文件或者用来写它的软件的人的怜悯但是在这里你甚至可能想要忽略像“aaa”这样的线,它会通过我的强制性readLine。)