Java FileNotFoundExeception不工作
在我当前的项目中,我遇到了一个问题,就是没有接收到文件未找到异常。我的驱动程序文件将打开的路径传递给正在构建图书库的构造函数。我正在使用JFileChooser获取路径。在尝试强制执行错误(输入不存在的文件名)时,它构建的库中没有任何信息,并且不会引发错误 驱动程序代码:Java FileNotFoundExeception不工作,java,input,exception-handling,output,Java,Input,Exception Handling,Output,在我当前的项目中,我遇到了一个问题,就是没有接收到文件未找到异常。我的驱动程序文件将打开的路径传递给正在构建图书库的构造函数。我正在使用JFileChooser获取路径。在尝试强制执行错误(输入不存在的文件名)时,它构建的库中没有任何信息,并且不会引发错误 驱动程序代码: //open an existing library JFileChooser dlg = new JFileChooser ("LibraryData");
//open an existing library
JFileChooser dlg = new JFileChooser ("LibraryData");
FileNameExtensionFilter filter = new FileNameExtensionFilter ("Text Files", "txt");
dlg.setFileFilter(filter);
dlg.setDialogTitle("Select Existing File");
dlg.setApproveButtonToolTipText("Select the file you want to open and click me.");
int button = dlg.showOpenDialog(null);
if (button == dlg.APPROVE_OPTION)
{
currentPath = dlg.getSelectedFile().getPath();
library = new PersonalLibrary(currentPath);
System.out.println("===========================================================");
System.out.println("File opened successfully from: \n" + currentPath);
System.out.println("===========================================================");
}
Util.enterToContinue();
Util.clearScreen();
break;
public PersonalLibrary(String path)
{
try
{
File myFile = new File(path);
if (myFile.exists())
{
Scanner input = new Scanner(myFile);
while(input.hasNext())
{
//code that populates the library
}
input.close();
saveNeeded = false;
}
}
catch (FileNotFoundException e)
{
System.out.println("Error: " + e.getMessage());
}
库代码:
//open an existing library
JFileChooser dlg = new JFileChooser ("LibraryData");
FileNameExtensionFilter filter = new FileNameExtensionFilter ("Text Files", "txt");
dlg.setFileFilter(filter);
dlg.setDialogTitle("Select Existing File");
dlg.setApproveButtonToolTipText("Select the file you want to open and click me.");
int button = dlg.showOpenDialog(null);
if (button == dlg.APPROVE_OPTION)
{
currentPath = dlg.getSelectedFile().getPath();
library = new PersonalLibrary(currentPath);
System.out.println("===========================================================");
System.out.println("File opened successfully from: \n" + currentPath);
System.out.println("===========================================================");
}
Util.enterToContinue();
Util.clearScreen();
break;
public PersonalLibrary(String path)
{
try
{
File myFile = new File(path);
if (myFile.exists())
{
Scanner input = new Scanner(myFile);
while(input.hasNext())
{
//code that populates the library
}
input.close();
saveNeeded = false;
}
}
catch (FileNotFoundException e)
{
System.out.println("Error: " + e.getMessage());
}
如果检查文件是否存在,将永远不会执行catch块
if(myFile.exists())
如果它不存在,则不会执行任何其他操作,包括catch块。此代码块中无法发生FileNotFoundException。如果要捕获FileNotFoundException,请去掉If块。或者只添加一个else块,在文件不存在时,在那里处理您想做的任何处理。方法file#exists()
检查文件是否存在。如果是,则返回true并进入If块
由于文件不在那里,它只是跳过if块并继续。由于未尝试访问不存在的文件对象,因此不会引发异常
如果你想抛出一个异常,你必须自己这样做
if(file.exists()) {
//do file operation
} else {
throw new FileNotFoundException("Oops! No file...");
}