我正在尝试使用标记器将 .txt 文件的内容插入到向量中;但是没有插入特定文件。稍后我需要稍后将向量的内容传递给jtable。问题是.txt的第一行有空值,所以它没有被插入它跳过第一行并继续前进..是有没有办法在向量中存储 null 并用 " " 替换它?
private void loadSecondPreview() {
String aLine;
recordData = new Vector();
columnNames = new Vector();
try {
FileInputStream fin = new FileInputStream(dataFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
// extract column names
if (delimiter == ",") {
StringTokenizer st1 =
new StringTokenizer(br.readLine(), ",");
while (st1.hasMoreTokens()) {
columnNames.addElement(st1.nextToken());
}
// extract data
while ((aLine = br.readLine()) != null) {
StringTokenizer st2 =
new StringTokenizer(aLine, ",");
while (st2.hasMoreTokens()) {
recordData.addElement(st2.nextToken());
}
}
} else if (delimiter == "\t") {
StringTokenizer st1 =
new StringTokenizer(br.readLine(), "\t");
while (st1.hasMoreTokens()) {
if (st1.equals(null)) {
columnNames.addElement(" ");
} else {
columnNames.addElement(st1.nextToken());
}
}
// extract data
while ((aLine = br.readLine()) != null) {
StringTokenizer st2 =
new StringTokenizer(aLine, "\t");
while (st2.hasMoreTokens()) {
if (st2.equals(null)) {
recordData.addElement(" ");
} else {
recordData.addElement(st2.nextToken());
}
}
}
}
br.close();
System.out.println("Column vector size:" + columnNames.size());
System.out.println("Record vector size:" + recordData.size());
for (int x = 0; x < columnNames.size(); x++) {
System.out.println("Column vector:" + x + " " + columnNames.elementAt(x));
}
for (int x = 0; x < recordData.size(); x++) {
System.out.println("Record vector:" + x + " " + recordData.elementAt(x));
}
O/P:**列向量大小:0 记录向量大小:5 记录向量:0 8 记录向量:1 8.100 记录向量:2 8.200 记录向量:3 9 记录向量:4 9.8
测试文件记录:第一行有两个空值列
8 8.100 8.200 9 9.8