Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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 使用try-catch处理FileNotFoundException_Java - Fatal编程技术网

Java 使用try-catch处理FileNotFoundException

Java 使用try-catch处理FileNotFoundException,java,Java,话虽如此,我已经用一个单独的方法检查了一个文件是否存在,但是,我现在想通过递归地使用try-catch将上述内容压缩为一个方法: static boolean exists(String location) { File path = new File(location); if (path.exists()){ return true; } else { return false;

话虽如此,我已经用一个单独的方法检查了一个文件是否存在,但是,我现在想通过递归地使用try-catch将上述内容压缩为一个方法:

static boolean exists(String location) {
        File path = new File(location);
        if (path.exists()){
            return true;
        }
        else {
            return false;
        }
    }

static boolean checkFile(String location1) throws IOException {
        if ( exists(location1) ) {
            File file = new File(location1);
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            raf.seek( raf.length()-1 );
            byte lastByte = raf.readByte();
            raf.close();
            if (lastByte == 2) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            System.out.println("File does not exist, please try again: ");
            Scanner sk = new Scanner(System.in);
            String newLocation = sk.nextLine();
            checkFile(newLocation);
            sk.close();
            return false;
第一个问题:如何使用
文件文件
创建新的随机访问文件?在尝试
File=null之前,我确实尝试过初始化它,但随后它抛出NullPointer,因为在它捕获异常后,将再次调用该方法,并且该文件将自动为null

另一个想法是
边做边做的循环中尝试catch
,但我想边做什么?我必须检查该文件是否存在,考虑到我已经在使用
try catch
执行此操作,它变得多余

只要我输入一个不是文件的路径,然后用一种方法继续执行
if
语句,我能做什么来尝试catch

如果你不得不使用
try/catch
,我真的不推荐,你可以在“永远”循环中这样做

static boolean checkFile(String location) {

        try {
            File file = new File(location);
        }
        catch (FileNotFoundException e) {
            System.out.println("File does not exist, please try again: ");
            Scanner sk = new Scanner(System.in);
            String newLocation = sk.nextLine();
            checkFile(newLocation);
        }

        //the following if-sentence checking whether some byte checks up.
    }

像这样的方法应该会奏效:

public static FileReader readerForPromptedFile() {
    Scanner keyboard = new Scanner(System.in);
    while (true) {
        try {
            System.out.println("Please enter a file name");
            String fileName = keyboard.nextLine();
            return new FileReader(fileName);
        } catch (FileNotFoundException e) {
            System.out.println("File not found, please try again");
        }
    }
}

如果有一种合理的方法来检查它是否应该首先工作,那么使用一种可能抛出异常的方法通常是不受欢迎的——在这种情况下,该行为不是真正的异常,而是一种先决条件。它还增加了相当大的性能开销。

您必须使用try-catch吗?如果没有它,这个问题会容易得多。是的,有人特别声明使用try-catch来解决这个问题,不过,我自己也觉得这有点奇怪,因为当我们有n个方法都对文件执行不同的操作时,有一个方法来检查文件的存在不是更容易吗?与“答案”无关但是我不能放过它:考虑冷凝<代码>中的“I/OR”存在<代码> >返回(新文件(位置))。哦,谢谢你的建议,看起来整洁多了。
static boolean checkFile(String location) {

    while (!exists(location)){
        System.out.println("File does not exist, please try again: ");
        Scanner sk = new Scanner(System.in);
        String newLocation = sk.nextLine();         
    }

    try {
    //the following if-sentence checking whether some byte checks up.
    }
    catch (IOException e) {
        // Handle exception
    }
}