【发布时间】:2016-08-18 12:30:13
【问题描述】:
我正在尝试通过单击按钮使用 WindowBuilder 将文本文件中的数据加载到 JList 中。正如您从下面的代码中看到的那样,我遇到了一些我似乎无法修复的异常。导入 java.io.FileReader 没有帮助。
我有一个单独的类文件,其中包含我的分数向量的代码。
JButton btnLoadData = new JButton("Load Data");
btnLoadData.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String sLine;
Vector<Score> scoreVec = new Vector<Score>();
//Here I am getting a FileNotFoundException for (new FileReader("scores.txt")
BufferedReader in = new BufferedReader(new FileReader("scores.txt"));
//Here I am getting an IOException for in.readLine()
while ((sLine = in.readLine()) != null)
{
scoreVec.addElement(new Score(sLine));
}
//this is also throwing an IOException
in.close();
//Placeholders until I get the rest of the program working
System.out.println(scoreVec.get(1).name);
System.out.println(scoreVec.get(1).country);
}
});
btnLoadData.setBounds(10, 227, 89, 23);
frame.getContentPane().add(btnLoadData);
【问题讨论】:
-
new FileReader("scores.txt")将高分放在保证可读的稳定路径(a 的子目录)中。该路径将是user.home。
标签: java file filenotfoundexception ioexception