这是我想要在控制台上阅读和显示的文本文件。以下是我阅读文件的代码:
public class CarMain
{
public static void main (String args[]) throws IOException
{
try
{
File f = new File("Cars.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String line = "", fullName;
String[] arrName = null;
Car[] c = new Car[3];
String name, ic, manufacturer, model;
int num = 0;
while ((line = br.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(line);
name = st.nextToken();
StringTokenizer st2 = new StringTokenizer(line,"://;");
ic = st2.nextToken();
manufacturer = st2.nextToken();
model = st2.nextToken();
c[num] = new Car(name,ic, manufacturer, model);
num++;
}
br.close();
fr.close();
for (int i = 0; i < c.length; i++)
{
System.out.println(c[i].toString());
}
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null,"error opening file");
}
}
}
然后我得到了这个输出:
似乎在&#34 ;;&#34;之后它没有显示模型名称。并且数据不在正确的位置。
我的预期输出是这样的:
Name: Fatimah Zahra Ali
IC: 860802105012
Manufacturer: Proton
Model: Perdana
.
.
.
请帮忙。谢谢。