【发布时间】:2014-12-15 07:13:37
【问题描述】:
这是我的代码。当我给它一个不存在的文件名时,由于某种原因没有抛出 FileNotFoundException。
public static String Question1( String fileName )
{
String message = "";
if ( fileName == null )
{
fileName = "files/question1/sample.txt";
}
try
{
Scanner fileScan = new Scanner( new File ( fileName ));
while ( fileScan.hasNext() )
{
String readLine = fileScan.nextLine();
if ( message.equals( "" ) )
{
message = readLine + "\n";
}
else
{
message = message + readLine + "\n";
}
}
}
catch ( FileNotFoundException e )
{
message = "Error: Could not find file!";
}
return message;
}
当我使用不存在的文件名运行代码时,返回的消息是“”而不是“错误:找不到文件!”
【问题讨论】:
-
你调试过代码,确定异常被捕获了吗?
-
很明显文件确实存在。 很明显。
标签: java filenotfoundexception