Swing GUI IOException 和 FileNotFoundException
Swing GUI IOException and FileNotFoundException
我正在尝试通过单击按钮使用 WindowBuilder 将文本文件中的数据加载到 JList
。正如您从下面的代码中看到的,我遇到了一些我似乎无法修复的异常。导入 java.io.FileReader
没有帮助。
我有一个单独的 class 文件,其中包含我的得分向量的代码。
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);
相关class和scores.txt
的真实路径是什么
我不推荐,但只有一个你可以使用 scores.txt 文件的完整路径。
您需要捕获并处理引发的异常,例如...
public void actionPerformed(ActionEvent arg0) {
String sLine;
Vector<Score> scoreVec = new Vector<Score>();
//Here I am getting a FileNotFoundException for (new FileReader("scores.txt")
try (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));
}
} catch (IOException exp) {
exp.printStackTrace();
}
//Placeholders until I get the rest of the program working
System.out.println(scoreVec.get(1).name);
System.out.println(scoreVec.get(1).country);
}
有关详细信息,请参阅 Exceptions trail and The try-with-resources Statement
我正在尝试通过单击按钮使用 WindowBuilder 将文本文件中的数据加载到 JList
。正如您从下面的代码中看到的,我遇到了一些我似乎无法修复的异常。导入 java.io.FileReader
没有帮助。
我有一个单独的 class 文件,其中包含我的得分向量的代码。
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);
相关class和scores.txt
的真实路径是什么我不推荐,但只有一个你可以使用 scores.txt 文件的完整路径。
您需要捕获并处理引发的异常,例如...
public void actionPerformed(ActionEvent arg0) {
String sLine;
Vector<Score> scoreVec = new Vector<Score>();
//Here I am getting a FileNotFoundException for (new FileReader("scores.txt")
try (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));
}
} catch (IOException exp) {
exp.printStackTrace();
}
//Placeholders until I get the rest of the program working
System.out.println(scoreVec.get(1).name);
System.out.println(scoreVec.get(1).country);
}
有关详细信息,请参阅 Exceptions trail and The try-with-resources Statement