我创建了一个类,该类使您可以将文件信息存储在每行的单独数组中,并分别显示它们。我想知道是否有一种方法可以从此数据的不同标题下创建JTable,以便可以将其从此信息转换为表,以后可以通过执行诸如从该文件添加和删除行之类的操作来操纵它? 这是我所做的如此之遥:
public class STIAdemo {
private static BufferedReader br;
public static void main(String[] args) throws Exception {
br = new BufferedReader(new FileReader("Roadslist"));
ArrayList<String[]> lines = new ArrayList<String[]>();
for(String line = br.readLine();line != null;line = br.readLine()) {
//comma will be replaced with .
line=line.replaceAll(",","\\.");
//each line will be stored in an array with . as separator
String[] fields = line.split("\\.");
//System.out.println(" " + fields[0]);
lines.add(fields);
}
String[][] strings = (String[][]) lines.toArray(new String[lines.size()][]);
System.out.println("Total Lines: " + strings.length);
//code to display
for(String s[]:strings){
for(String ss:s){
System.out.print(ss+", ");
}
System.out.println();
}
}
}
我在类似的道路上有信息
Epinal Way 0, Alan Moss Rd Roundabout, Epinal-Garendon, 0.16, 40, no, -1, flawless
Epinal Way 1, Epinal-Garendon, Ashby Rd Roundabout, 0.13, 36, no, -1, impaired
Epinal Way 2, Ashby Rd Roundabout, Epinal-University, 0.1, 36, no, -1, flawless
Epinal Way 3, Epinal-University, Epinal-Radmoor, 0.13, 36, no, -1, flawless
Epinal Way 4, Epinal-Radmoor, Epinal-Forest, 0.3, 36, no, -1, flawless
Westfield Drive, Ashby Rd Roundabout, Radmoor-Westfield, 0.39, 20, no, -1, flawless, yes, yes, yes, no, no, no
,我想将其转换为一个类似于以下内容的表:第一行的类别对应于每个字段的部分。
Name Intersection 1 Intersection 2 Road name 3 Distance Speed one way? height, Epinal Way 0, Alan Moss Rd Roundabout, Epinal-Garendon, 0.16, 40, no, -1, flawless