我一直在制作一个 GUI 程序,我正在调用一个引发异常的方法。我必须使用try and catch,因为我无法编辑代码,因为它的GUI。由于某种原因,代码不起作用。该代码应该获取文件的第一行并在标签中显示数字。这是代码:
try{
String inputStr = JOptionPane.showInputDialog(this, "Enter The File Name: ");
int x= codeReadFile(inputStr);
String name= String.valueOf(x);
chipCount.setText(name);
}
catch (IOException e) {
JOptionPane.showMessageDialog(this,"No File");
}
文件读取程序的代码是:
public static int codeReadFile (String filename) throws IOException,
FileNotFoundException {
String line=null;
int value=0;
BufferedReader inputReader = new BufferedReader (new
InputStreamReader(new FileInputStream(filename)));
while (( line = inputReader.readLine()) != null)
value = Integer.parseInt(line);
inputReader.close();
return value;