你如何循环try/catch语句?我正在制作一个使用扫描仪读取文件的程序,它正在从键盘上读取它.所以我想要的是如果文件不存在,程序会说"这个文件不存在请再试一次".然后让用户键入不同的文件名.我尝试了几种不同的方法尝试这样做但是,我的所有尝试最终导致程序崩溃.
这就是我所拥有的
try {
System.out.println("Please enter the name of the file: ");
Scanner in = new Scanner(System.in);
File file = new File(in.next());
Scanner scan = new Scanner(file);
} catch (Exception e) {
e.printStackTrace();
System.out.println("File does not exist please try again. ");
}
Run Code Online (Sandbox Code Playgroud)