I'm new to Java and I'm porting one of my C++ libraries to Java as a learning experiment. This is not homework (as should be obvious from looking at my code). I have a few questions concerning the following code of my constructor for an ESRI shape file reader.
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); } } During parameter validation and existence should I be throwing the exceptions as shown? The validation throws unchecked exceptions, and the existence throws checked exceptions. I assume that FileInputStream constructor will also throw an IOException, but I specified that in the method throws clause.
I was considering refactoring the opening of the file to a seperate function, but I figured it would be more useful and simple to do this in the constructor, and also learns me how to control errors here. Besides, any instance of this object will not have a closed/open state. All of these objects are reserved strictly for READING a file only, so they are created on a as-needed basis per file. I will provide a close() method seperately.
Also, from an extensibility point of view, would it be difficult to adapt to reading a file over a network using the FileInputStream with the current constructor? Or should I use a different class and multiple constructors?
Thanks for any and all input.