java - Java 新手 : Constructor error control

标签 java exception parameters error-handling constructor

我是 Java 的新手,我正在将我的一个 C++ 库移植到 Java 作为学习实验。这不是家庭作业(从我的代码中可以明显看出)。关于 ESRI 形状文件阅读器的构造函数的以下代码,我有几个问题。

import java.io.*;

/**
 *
 * @author Bill
 */
public class ShapeFileReader {
    public FileInputStream inStream;
    /*
     * @param fileName File name string. Must not be null or zero length.
     * @throws Exception if file specified by fileName fails to open
     */
    public ShapeFileReader(String fileName) throws IOException {
        if(fileName == null)
            throw new NullPointerException("fileName was null");

        if(fileName.length() == 0)
            throw new IllegalArgumentException("fileName string length was zero");

        File fi = new File(fileName);
        if(fi.exists() == false)
            throw new IOException("File does not exist: " + fileName);

        // Catch-or-specify (this method already throws IOException)
        inStream = new FileInputStream(fileName);
    }
}

在参数验证和存在期间,我是否应该如图所示抛出异常?验证抛出未检查异常,存在抛出检查异常。我假设 FileInputStream 构造函数也会抛出 IOException,但我在方法 throws 子句中指定了它。

我正在考虑将文件的打开重构为一个单独的函数,但我认为在构造函数中执行此操作会更有用和简单,并且还让我了解了如何在此处控制错误。此外,该对象的任何实例都不会具有关闭/打开状态。所有这些对象都严格保留用于读取文件,因此它们是根据每个文件的需要创建的。我会单独提供一个close()方法。

此外,从可扩展性的角度来看,是否很难适应使用当前构造函数使用 FileInputStream 通过网络读取文件?还是应该使用不同的类和多个构造函数?

感谢所有的输入。

最佳答案

我不会理会异常,FileInputStream 会为您抛出异常,除了让代码变得困惑之外,您不会添加任何东西。

为了让它与网络一起工作,而不仅仅是您要修改的文件:

public class ShapeFileReader {
    private final InputStream inStream;

    public ShapeFileReader(InputStream inStream) {
        this.inStream = inStream;
    }

    /*
     * @param fileName File name string. Must not be null or zero length.
     * @throws Exception if file specified by fileName fails to open
     */
    public ShapeFileReader(String fileName) throws IOException {
        this(new FileInputStream(fileName));
    }

由于这已被接受为答案,我正在编辑它,因为 Roland(在评论中)是非常正确的,这不是我处理问题的方式。

 public class ShapeReader {
    public static Shape readShape(InputStream inStream) {
        ... do the work
    }

    /*
     * @param fileName File name string. Must not be null or zero length.
     * @throws Exception if file specified by fileName fails to open
     */
    public static Shape readShape(String fileName) throws IOException {
        FileInputStream fis = new FileInputStream(fileName);
        try {
            return readShape(fis);
        } finally {
            fis.close();
        }
    }
}

关于java - Java 新手 : Constructor error control,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8018491/

相关文章:

java - 找不到符号方法Android "findviewbyID"

java - org.hibernate.ObjectNotFoundException : No row with the given identifier exists

.net - 如何阻止机器人发布并导致异常

java - 堆栈跟踪中的 request.postData 是什么

java - UncaughtExceptionHandler 更改异常消息

python - 如何修复 "ImportError: Unable to load EGL library',22,找不到指定的模块”

php - Symfony 中的 URL 会自动添加我的参数 ?Symfony=

parameters - Jenkins触发时间参数插件

visual-studio-code - VSCode Intellisense 在 Jupyter Notebook Extension(MacOS) 中不起作用

java - java的正则表达式