有没有办法使用try/catch语句让用户输入一个文件,如果用户输入了错误的文件名,程序会再问两次,然后异常退出?我怎么能循环?因为一旦用户输入了错误的文件名,程序就会立即抛出异常。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
static String[] words = new String[5];
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.println("enter file name:");
String fileName = kb.next();
try {
File inFile = new File(fileName);
Scanner in = new Scanner(new File(fileName));
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}