我有这两种方法可以从文件中读取多个整数并将它们插入树中。如果找到文件,它工作正常,但如果找不到文件,它不会打印“找不到文件”。为什么它不进入 catch 语句?谢谢!
public static void openF(Tree myT)
{
try
{
x=new Scanner(new File("Number.txt"));
readF(myT);
}
catch(Exception e)
{
System.out.println("File not found");
}
}
// to read from the file
public static void readF(Tree myT)
{
while(x.hasNext()) //keeps going till it reaches the end of file
{
int a =x.nextInt();
myT.insert(a); //insert in tree
}
}