我想问一下我在尝试创建一个可以从文件中读取文本的方法时遇到的问题。
例如,我创建了一个简单的界面,当您单击按钮时,将读取带有预定义文件夹路径的文本。
所以我像这样使用actionListener。请注意,“einlesen”是德语中的“阅读”。
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == einlesenDatei)
{
this.einlesen();
}
if (source == decoder)
{
this.decode();
}
}
问题是,readInput
方法要求我抛出FileNotFoundException
,而actionPerformed
方法要求我切断抛出异常部分。
答案 0 :(得分:2)
您可以将read
方法的代码放在try catch
块中,如下所示:
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == inputFile) {
try {
this.readInput();
} catch (FileNotFoundException e) {
// handle the exception
}
}
if (source == decoder) {
this.decode();
}
}