Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6799983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:54:50+00:00 2026-05-26T18:54:50+00:00

I’m new to Java and I’m porting one of my C++ libraries to Java

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:54:51+00:00Added an answer on May 26, 2026 at 6:54 pm

    I wouldn’t bother with the exceptions, FileInputStream will throw an exception for you, you’re not adding anything other than clutter to your code.

    For it to work with the network rather than just a file you’d modify thus:

    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));
        }
    

    Since this has been accepted as the answer I’m editing it as Roland (in the comments) is quite correct and this isn’t how I’d have approached the problem.

     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();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.