在Java中使用try-catch时的变量范围问题

时间:2015-06-23 18:01:57

标签: java scope

我有一个类PDF,它实现了一个接口fileReader

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class PDF implements fileReader {
    @Override
    public byte[] readFile(File pdfDoc) {
        if (!pdfDoc.exists()) {
            System.out.println("Could not find" + pdfDoc.getName() + " on the specified path");
            return null;
        }
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(pdfDoc);
        } catch (FileNotFoundException e) {
            System.out.println("");
            e.printStackTrace();
        }
        byte fileContent[] = new byte[(int) pdfDoc.length()];
        try {
            fin.read(fileContent);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileContent;
    }
}

import java.io.File;
public interface fileReader {
    <T> T readFile(File fileObject);
}

我注意到变量fin存在范围问题。

我做的另一个实现是:

public byte[] readFile1(File pdfDoc) {
        if (!pdfDoc.exists()) {
            System.out.println("Could not find" + pdfDoc.getName() + " on the specified path");
            return null;
        }
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(pdfDoc);
            byte fileContent[] = new byte[(int) pdfDoc.length()];
            try {
                fin.read(fileContent);
            } catch (IOException e) {
                System.out.println("");
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            System.out.println("");
            e.printStackTrace();
        }
        return fileContent;
    }

但现在我无法访问fileContent

如何合并try-catches以便我没有范围问题? 有没有更好的设计方法来解决这个问题?我必须创建用于读取三种不同类型文件的函数。

6 个答案:

答案 0 :(得分:5)

从Java 7开始,您可以将fuel/application/config/MY_fuel.php组合如下:

$config['modules_allowed'] = array('blog');

在我看来,这使代码更清晰,变量范围更明显。

答案 1 :(得分:3)

您可以嵌套try catch语句:

    try {
       FileInputStream fin = new FileInputStream(pdfDoc);
       byte fileContent[] = new byte[(int) pdfDoc.length()];
       try {
          fin.read(fileContent);
          return fileContent;
       } catch (IOException e) {
         e.printStackTrace();
       } finally {
         fin.close();
       }

    } catch (FileNotFoundException e) {
        System.out.println("");
        e.printStackTrace();
    }
    return null;

请注意,我在finally子句中添加了close()来清理。如果出现错误,那么返回null可能不是您想要的,而是特定于应用程序。

答案 2 :(得分:2)

您可以拥有一个trycatch块。

try {
    //do stuff
}
catch (FileNotFoundException e) {
        System.out.println("");
        e.printStackTrace();
}
catch (IOException e) {
        e.printStackTrace();
}

答案 3 :(得分:2)

您可以修改此部分:

        FileInputStream fin = null;
        try {
            fin = new FileInputStream(pdfDoc);
        } catch (FileNotFoundException e) {
            System.out.println("");
            e.printStackTrace();
        }
        byte fileContent[] = new byte[(int) pdfDoc.length()];
        try {
            fin.read(fileContent);
        } catch (IOException e) {
            e.printStackTrace();
        }

通过

{
......
       FileInputStream fin = null;
       byte fileContent[]=null;
        try {
            fin = new FileInputStream(pdfDoc);
            fileContent = new byte[(int) pdfDoc.length()];
            fin.read(fileContent);
        } catch (FileNotFoundException e) {
            System.out.println("");
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return fileContent
    }

答案 4 :(得分:1)

我会这样写:

public byte[] readFile(File pdfDoc) {
    if (!pdfDoc.exists()) {
        System.out.println("Could not find" + pdfDoc.getName() + " on the specified path");
        return null;
    }
    FileInputStream fin = null;
    byte fileContent[] = new byte[(int) pdfDoc.length()];

    try {
        fin = new FileInputStream(pdfDoc);
        fin.read(fileContent);
    } catch (FileNotFoundException e) {
        System.out.println("");
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != fin) {
            fin.close();
        }
    }   
    return fileContent;
}

答案 5 :(得分:0)

从Java 7开始,有一个很好的实用方法可以读取文件的整个内容:

return Files.readAllBytes(pdfFile.toPath());

此方法将为您打开和关闭FileInputStream,因此您无需自己执行此操作。如果出现问题,它会抛出IOException。通常,最好让此异常传播给调用者,但如果您确实想在这种情况下返回null,则可以按如下方式完成此操作:

try {
    return Files.readAllBytes(pdfFile.toPath());
} catch (IOException e) {
    e.printStackTrace();
    return null;
}

这也有一个很好的优点,就是在这种情况下返回的值是显式的 - 或者你是否真的想要返回一个填充0值的数组,如果找不到文件,就像你当前的代码那样?

请注意,由于NoSuchFileException是IOException的子类,因此catch块将处理这两者。如果你想以不同的方式处理它,你可以为NoSuchFileException写一个单独的catch块:

try {
    return Files.readAllBytes(pdfFile.toPath());
} catch (NoSuchFileException e) {
    System.err.println("Oh no, the file has disappeared.");
    e.printStackTrace();
    return null;
} catch (IOException e) {
    System.err.println("The file exists, but could not be read.");
    e.printStackTrace();
    return null;
}

最后,我可能会提到您的文件读取代码不正确,因为InputStream.read()不一定会读取整个文件。这就是为什么它返回读取的字节数,以便您可以再次为文件的其余部分调用它。但正如我所说,自Java 7以来,您不需要使用这样的低级API(当然,除非文件太大而无法容纳到内存中)。