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 5 of 5

Thread: data and object streams

  1. #1
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default data and object streams

    hi all,

    java newbie here - reading through the java tutorials and a little confused here.

    i've read that using a data stream means that the values are represented in an internal binary format. does this mean if you use a data stream to output some int values to a file, you won't be able to read the data in the file?

    let's say you want to output some int values to a .txt file: 5, 10, and 25.

    Why would you use a data stream and not a byte or character stream?

    also, can anyone provide a simple explanation of an object stream and an example of when you would use it?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,620
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: data and object streams

    won't be able to read the data in the file
    You can read the data with a data stream. The data written will NOT be ASCII text.

    want to output some int values to a .txt file: 5, 10, and 25.
    What format/representation for the data do you want in the .txt file? I would assume you'd want ASCII characters in a .txt file. To get those you need to write Strings.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,308
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: data and object streams

    Quote Originally Posted by bbr201 View Post
    also, can anyone provide a simple explanation of an object stream and an example of when you would use it?
    Object streams allow you to save an object. An example might be for a program to save some internal state to be opened up at a later time, in the simplest case it can save one or more user preferences, in more complex cases save session and data values in J2EE web applications. The class must implement Serializable to be able to be saved in this manner.

  4. The Following User Says Thank You to copeg For This Useful Post:

    bbr201 (July 16th, 2010)

  5. #4
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Re: data and object streams

    Quote Originally Posted by Norm View Post
    What format/representation for the data do you want in the .txt file? I would assume you'd want ASCII characters in a .txt file. To get those you need to write Strings.
    What advantage is there to using a data stream to output and read primitive data types?

    In the case of a text file, it makes sense that you would want ASCII characters in the event that a human being wants to open the file and see the contents. So I assume in this case it would be best to work with a character stream.

    But why ever choose the data stream then?

    In other words, let's say you use another file to read and store values 'data.dat'. No human being will ever need to open the file and see the contents. It is only used by a program to store (and later read) int values. In this case, couldn't you use a byte, character, or data stream? How would you choose which to use?

    I guess I am just confused in general. :-(

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: data and object streams

    Writing out as a binary file prevents you from manually having to figure out how to de-serialize the data into a useable object (i.e. re-create that object from the data). Java has a default serialization method that allows you to just open an object stream and then write out the file, instead of specifying "I want value A written first, then value B is written in this format,..." etc. etc.

    Objects stored in binary are also smaller (in general) than the text representation of that object.

    For example:

    Say you have the double value 1.23456789012345e-126

    To write that value out as a string, it would take 21 bytes (depending on the encoding), where as a normal double takes only 10 bytes of memory. That's more than twice the amount of data you need to process. For this simple example, it may not matter but I have worked on data that has millions of numbers (a lot of data sets even go into the billions, trillions, or higher), and for these data sets having twice the data to process is simply unacceptable. Also, it is extremely quick to serialize/deserialize a binary object compared to parsing a string representing the object.

    And sometimes you just don't want humans to be able to open up and mess around with the files you're saving out

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    bbr201 (July 16th, 2010)

Similar Threads

  1. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM
  2. Help with object
    By Spangle1187 in forum Collections and Generics
    Replies: 3
    Last Post: April 22nd, 2010, 04:34 PM
  3. object not accessible
    By sheikhms60 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 22nd, 2010, 06:37 PM
  4. reading JMF input streams?
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 11th, 2010, 11:22 PM
  5. Object o = new Object(); need help
    By zeeshanmirza in forum Object Oriented Programming
    Replies: 11
    Last Post: January 6th, 2010, 09:01 PM