捕获异常时,运行:
JOptionPane.showMessageDialog("File is corrupted. Please select a new file.");然后再次显示文件对话框。
最好将其作为一个循环,在文件无效时继续执行此操作。如果存在损坏,则设置布尔标志并循环(只要设置了该标志),而不是引发异常。这样,当找到一个好的文件时,while循环将终止。
例:
public static void main(String[] args){ boolean goodFile = false; while (!goodFile){ JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(); goodFile = processFile(chooser.getSelectedFile()); }}private boolean processFile(File file){ //do you stuff with the file //return true if the processing works properly, and false otherwise }


