Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Tolls

Thread: FileOutputStream Question

  1. #1
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default FileOutputStream Question

    Java Code:
    //Option 1
    FileOutputStream fileStream = new FileOutputStream("Students.dat");
    ObjectOutputStream os = new ObjectOutputStream(fileStream));
    
    // Option 2
    ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Students.dat")));
    I have just stareted learning about serialisation/deserialisation in java and I was wondering which option is better for the above code?
    Option 1 is longer but probably more clear but option 2 is more concise.


    Also on a separate question about creating instances of Inner classes should I use this syntax
    Java Code:
    OuterClass.InnerClass innerObject= outerObject.new InnerClass();
    Or should I just have a public method in the class which contains the inner class which can create an instance of the Inner class and return it?
    Any feedback would be appreciated.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: FileOutputStream Question

    In the first case, either is acceptable. If you had too many instance creations within a single statement it might get confusing trying to catch the various exceptions but it's really up to you how to handle it.

    As far as creating a instance of an inner class, either way is also okay. The problem using a method to return an instance is that it might require separate overloaded methods to accommodate different constructors of the inner class.

    Regards,
    Jim
    Last edited by jim829; 10-08-2017 at 10:22 PM. Reason: grammar
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    2

    Default Re: FileOutputStream Question

    Java Code:
    ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Students.dat")));
    So it means that:
    FileOutputStream("Students.dat")
    this FileOutputStream is only used once? ( only in parameters )
    And nowhere else it can be reused anymore, right? They are glued.

  4. #4
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default Re: FileOutputStream Question

    Thanks for the reply Jim, I thought both approaches would be ok but just wanted to check as I have been tryign to not learn any bad habits with programming.

    Also thank you for the reply asdfg although I am not entirely sure what you are trying to say.
    If I understand what you are saying correctly, Yes to my knowledge the FileOutputStream object is only used through the ObjectOutputStream
    (The ObjectOutputStream holds(At least I assume it stores it) the only active reference to the FileOutputStream object created).
    Although to be honest I am not really sure what either of these classes do. I know it essentially just saves all the values of an objects variables at a point so they can be accessed later on by storing them in a file but it is like a black box for me I have no idea how the two classes(FileOutputStream,ObjectOutputStream) actually work.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: FileOutputStream Question

    In general I would question code that had something like this:
    Java Code:
    OuterClass.InnerClass innerObject= outerObject.new InnerClass();
    as to me the inner class should be something handled by the owning object itself, not by something external to that object, so this code should not really appear.

    I know you can do it, but that doesn't mean you should do it.
    SurfMan likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. FileOutputStream
    By Andrea in forum New To Java
    Replies: 3
    Last Post: 08-04-2013, 03:25 AM
  2. Clarification of FileOutputStream
    By rogi85 in forum New To Java
    Replies: 4
    Last Post: 08-03-2011, 12:02 PM
  3. FileOutputStream to GIF
    By dewitrydan in forum New To Java
    Replies: 12
    Last Post: 10-11-2010, 07:12 AM
  4. FileOutputStream gets NotSerializableException
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 09:38 PM
  5. FileOutputStream question...
    By SCS17 in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 05:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •