Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Dont understand objects used with FileInputStream and FileOutputStream constructor

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Dont understand objects used with FileInputStream and FileOutputStream constructor

    In the code below, both fin and fout variables are set to null. Why is this. In the second and third "try block" fin and fout are compared to "null". Why is this? Please help me with my question.

    Thanks,
    Truck35

     // Copy a File.
        try {
          // Attempt to open the files.
          fin = new FileInputStream(args[0]);
          fout = new FileOutputStream(args[1]);
     
          do {
            i = fin.read();
            if(i != -1) fout.write(i);
          } while(i != -1);
     
        } catch(IOException exc) {
          System.out.println("I/O Error: " + exc);
        } finally {
          try {
            fin != null) fin.close();    //code in question
          } catch(IOException exc) {
            System.out.println("Error Closing Input File");
          }
          try {
             if(fout != null) fout.close();   //code in question
            } catch(IOException exc) {
            System.out.println("Error Closing Output File");
          }
        }
      }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Dont understand objects used with FileInputStream and FileOutputStream constructor

    Please post code in the form of an SSCCE- this code does not contain the lines you're talking about, so it's pretty hard to answer your question.

    My guess would be that both streams are compared to null in case there was an Exception while attempting to create them. If they weren't created, you can't close them.

    They're initially set to null so that this comparison can happen in the first place. Java doesn't assign null to variables inside functions, so you have to specifically tell it to set the variable to null. If you didn't do that, this code wouldn't work - in fact you'd probably get a compilation error.

    But like I said, without an SSCCE, I'm just guessing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. My code has error when its run....I dont understand what's wrong of it.
    By jacky@~ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 11th, 2011, 07:48 AM
  2. Dont quite understand how Scanner.nextLine() works
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 14
    Last Post: September 21st, 2011, 05:27 PM
  3. I dont understand why this happens....
    By ashenwolf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 09:31 PM
  4. Dont understand what to write.. :/
    By johnwalker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 09:31 PM
  5. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM