Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 学习处理异常,can';我搞不懂这个FileNotFoundException_Java_Filenotfoundexception - Fatal编程技术网

Java 学习处理异常,can';我搞不懂这个FileNotFoundException

Java 学习处理异常,can';我搞不懂这个FileNotFoundException,java,filenotfoundexception,Java,Filenotfoundexception,我还是个初学者,目前正在学习处理异常。我想弄明白的书中的练习告诉我添加一个Finally块来关闭我打开的文件,我不明白我做错了什么。请记住,文件名和路径都是假的,但我有: public static String readLineWithFinally() { System.out.println("Starting readLineWithFinally method."); RandomAccessFile in = new RandomAccessFile("product

我还是个初学者,目前正在学习处理异常。我想弄明白的书中的练习告诉我添加一个Finally块来关闭我打开的文件,我不明白我做错了什么。请记住,文件名和路径都是假的,但我有:

public static String readLineWithFinally()
{
    System.out.println("Starting readLineWithFinally method.");
    RandomAccessFile in = new RandomAccessFile("products.ran", "r");
    try
    {                     
        String s = in.readLine();
        return s;
    } 
    catch (IOException e)
    {
        System.out.println(e.toString());
        return null;
    }
    finally
    {
        try
        {
                in.close();
        }
        catch (Exception e)
        {
            System.out.println("Generic Error Message");
        }
    }
}    

您发布的代码不应该编译,因为RandomFile(String,String)可能会抛出
FileNotFoundException
。因此,我们必须将其包含在
try
块中

System.out.println("Starting readLineWithFinally method.");
RandomAccessFile in = null;
try {
    in = new RandomAccessFile("products.ran", "r");
    String s = in.readLine();
    return s;
} catch (IOException e) {
    System.out.println(e.toString());
    return null;
} finally {
    try {
        if(in != null) {
            in.close();
        }
    } catch (Exception e) {
        System.out.println("Generic Error Message");
    }
}

您发布的代码不应该编译,因为RandomFile(String,String)可能会抛出
FileNotFoundException
。因此,我们必须将其包含在
try
块中

System.out.println("Starting readLineWithFinally method.");
RandomAccessFile in = null;
try {
    in = new RandomAccessFile("products.ran", "r");
    String s = in.readLine();
    return s;
} catch (IOException e) {
    System.out.println(e.toString());
    return null;
} finally {
    try {
        if(in != null) {
            in.close();
        }
    } catch (Exception e) {
        System.out.println("Generic Error Message");
    }
}
请记住,文件名和路径都是假的,但我有:

public static String readLineWithFinally()
{
    System.out.println("Starting readLineWithFinally method.");
    RandomAccessFile in = new RandomAccessFile("products.ran", "r");
    try
    {                     
        String s = in.readLine();
        return s;
    } 
    catch (IOException e)
    {
        System.out.println(e.toString());
        return null;
    }
    finally
    {
        try
        {
                in.close();
        }
        catch (Exception e)
        {
            System.out.println("Generic Error Message");
        }
    }
}    
这就是为什么在使用读取访问模式创建随机访问文件(“products.ran”、“r”)时,会出现
FileNotFoundException

如果模式为,此构造函数将抛出
FileNotFoundException
“r”
但给定的字符串并不表示现有的常规文件, 或者如果模式以“rw”开头,但给定的字符串不表示 现有的、可写的常规文件和该名称的新常规文件 无法创建,或者在打开或关闭时发生其他错误 创建文件

请记住,文件名和路径都是假的,但我有:

public static String readLineWithFinally()
{
    System.out.println("Starting readLineWithFinally method.");
    RandomAccessFile in = new RandomAccessFile("products.ran", "r");
    try
    {                     
        String s = in.readLine();
        return s;
    } 
    catch (IOException e)
    {
        System.out.println(e.toString());
        return null;
    }
    finally
    {
        try
        {
                in.close();
        }
        catch (Exception e)
        {
            System.out.println("Generic Error Message");
        }
    }
}    
这就是为什么在使用读取访问模式创建随机访问文件(“products.ran”、“r”)时,会出现
FileNotFoundException

如果模式为,此构造函数将抛出
FileNotFoundException
“r”
但给定的字符串并不表示现有的常规文件, 或者如果模式以“rw”开头,但给定的字符串不表示 现有的、可写的常规文件和该名称的新常规文件 无法创建,或者在打开或关闭时发生其他错误 创建文件


为了补充Taylor Hx的答案,您可以利用Java 7的构造来避免在您的案例中使用
finally

public static String readLineWithFinally() {
    System.out.println("Starting readLineWithFinally method.");
    try (RandomAccessFile in = new RandomAccessFile("products.ran", "r")) {
        return in.readLine();
    } catch (IOException e) {
        System.out.println(e.toString());
        return null;
    }
}

您还需要确保您的用法与for
RandomAccessFile

一致。要补充Taylor Hx的答案,您可以利用Java 7的构造,以避免在您的案例中最终完全使用

public static String readLineWithFinally() {
    System.out.println("Starting readLineWithFinally method.");
    try (RandomAccessFile in = new RandomAccessFile("products.ran", "r")) {
        return in.readLine();
    } catch (IOException e) {
        System.out.println(e.toString());
        return null;
    }
}

您还需要确保您的使用与for
RandomAccessFile

一致,您希望实现的是什么??。。。您面临的问题是什么???它可能正如您所期望的那样工作。如果它是一个虚构的文件名,
catch
块中的
System.out.println()
应该返回
e
的内容,这很可能是“FileNotFound”。文件名是作为SSCCE的一部分虚构的,还是在运行代码时使用了假文件名<代码>FileNotFoundException
通常在代码找不到文件时引发。这可能是因为路径指定不正确,或者提供的路径不正确,等等。这是一本书中的练习,因此除了教我如何处理异常之外,没有其他真正的编程目标。我需要它正确编译,并且不抛出任何异常(我编写的异常除外!)。您想要实现的是什么??。。。您面临的问题是什么???它可能正如您所期望的那样工作。如果它是一个虚构的文件名,
catch
块中的
System.out.println()
应该返回
e
的内容,这很可能是“FileNotFound”。文件名是作为SSCCE的一部分虚构的,还是在运行代码时使用了假文件名<代码>FileNotFoundException通常在代码找不到文件时引发。这可能是因为路径指定不正确,或者提供的路径不正确,等等。这是一本书中的练习,因此除了教我如何处理异常之外,没有其他真正的编程目标。我需要它正确编译,并且不会抛出任何异常(我编写的异常除外!)。不。。。然后中的
将不再在
finally
块的作用域中,代码将不会编译。修复并编辑。谢谢你接电话。我有点紧张。这很有效,我把它标记为正确答案,但我不明白的是我做错了什么。这本书实际上特别告诉我要将“RandomAccessFile in=new RandomAccessFile(“products.ran”,“r”)”移到try块之外(我开始练习时,它最初就在try块内部)。为什么在外部创建变量并为其赋值null,然后在try语句内部赋值新对象会起作用?@lfernandes如回答中所述,在
try
块外部使用
RandomAccessFile(String,String)
将无法编译,因为有可能
RandomAccessFile(String,String)
将抛出一个
FileNotFoundException
。在编译器接受您的代码之前,必须由您的程序处理此异常。在上面的代码中,我们将
In
初始化为
null
以保留范围(这样在第二个
块中可以看到
中的
try
,然后捕获构造函数调用中的任何异常。不…然后
中的
将不再在
最终
块中的作用域中,代码将不会编译。修复并编辑。感谢您捕获。有点困难。这很有效,我将其标记为答案正确,但我不明白的是我做错了什么。这本书实际上特别告诉我将“RandomAccessFile in=new RandomAccessFile(“products.ran”,“r”)”移到try块之外(我开始练习时它本来就在try块内)。为什么要在外部创建变量并为其赋值null,然后assi