Results 1 to 6 of 6

Thread: Data Streams and Object Streams in Java

  1. #1
    Join Date
    Aug 2006
    Posts
    222

    Data Streams and Object Streams in Java

    Hello Friends,
    I have done some basic coding in Java programming language. I know that a Java program uses a stream to either read data items from a source or to write data items to a destination. But in streams I am not able to get the data and object streams. So thought that posting here would be helpful. Please tell me what are the Data Streams and Object Streams in Java.? Waiting for your replies..!!
    Just a reply to say thank you for these links and posts I have a lot to read and learn now!



  2. #2
    Join Date
    Aug 2006
    Posts
    235

    Re: Data Streams and Object Streams in Java

    Data Streams and Object Streams are the types of the I/O Streams. An I/O Stream represents an input source or an output destination. Many different kinds of sources like disk files, devices, memory arrays, etc. are represented by a stream. Data Streams support binary I/O of primitive data type values and String values. In the data type value can contain boolean, char, byte, short, int, long, float, and double. Object streams support I/O of objects just like data streams support I/O of primitive data types. Java divides streams into input and output categories. Java also divides streams into byte-oriented and character-oriented categories. Hope that you got the basics about the data streams and object streams.
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  3. #3
    Join Date
    Jul 2006
    Posts
    286

    Re: Data Streams and Object Streams in Java

    Java's class library includes many stream classes. A problem with the FileInputStream and FileOutputStream classes is that they only work at the byte level. You can use the DataInputStream and DataOutputStream classes for reading integers, writing floating-point values, etc. The above mentioned classes are located in the java.io package portion of Java's standard class library. The data stream classes are designed so that their objects can be chained to other streams. All data streams implement either the DataInput interface or the DataOutput interface. The object stream classes are ObjectInputStream and ObjectOutputStream. These classes implement ObjectInput and ObjectOutput.
    IF you hate me, please don't mention it. I know who hates me and who doesn't. You really do not have to make fun of people....

  4. #4
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Data Streams and Object Streams in Java

    I have tried to explain the coding in program of the Data Streams. The following program defines some constants containing the name of the data file :
    Code:
    static final String dataFile = "dataexample";
    
    static final double[] prices = { 17.99, 11.99, 12.99, 7.99, 6.99 };
    static final int[] units = { 14, 7, 16, 22, 44 };
    static final String[] descs = { "Java Band",
            "Java Glass",
            "Duke Juggling Dolls",
            "Java Stapler",
            "Java Locket" };
    // Then DataStreams opens an output stream.
    
    out = new DataOutputStream(new
                BufferedOutputStream(new FileOutputStream(dataFile)));
    // DataStreams writes out the records and closes the output stream. 
    
    for (int i = 0; i < prices.length; i ++) {
        out.writeDouble(prices[i]);
        out.writeInt(units[i]);
        out.writeUTF(descs[i]);
    }

  5. #5
    Join Date
    Nov 2008
    Posts
    996

    Re: Data Streams and Object Streams in Java

    I have used the DataOutputStream and DataInputStream to write and read non-byte-oriented data items in my code. Hope that you will get some idea from the below coding :
    Code:
    import java.io.*;
    
    class IODemo
    {
      public static void main (String [] args)
      {
       DataOutputStream dstream = null;
    
       try
       {
         FileOutputStream fos = new FileOutputStream ("example.dat");
         dstream = new DataOutputStream (fos);
    
         dstream.writeInt (256);
         dstream.writeDouble (Math.PI);
         dstream.writeUTF ("Java");
    
       }
       catch (IOException e)
       {
         System.out.println (e.getMessage ());
         return;
       }
       finally
       {
         if (dstream != null)
           try
           {
             dstream.close ();
           }
           catch (IOException e)
           {
           }
       }
    
       DataInputStream dis = null;
    
       try
       {
         FileInputStream fis = new FileInputStream ("example.dat");
         dis = new DataInputStream (fis);
    
         System.out.println (dis.readInt ());
         System.out.println (dis.readDouble ());
         System.out.println (dis.readUTF ());
       }
       catch (IOException e)
       {
         System.out.println (e.getMessage ());
         return;
       }
       finally
       {
         if (dis != null)
           try
           {
             dis.close ();
           }
           catch (IOException e)
           {
           }
       }
      }
    }

  6. #6
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Data Streams and Object Streams in Java

    In many cases you have to write an object to a stream twice, you're really writing only the reference twice. Just glace the following code :
    Code:
    Object obj = new Object();
    out.writeObject(obj);
    out.writeObject(obj);
    If you want to match each writeObject by a readObjec, then the code that reads the stream back will look something like this :
    Code:
    Object obj1 = in.readObject();
    Object obj2 = in.readObject();

Similar Threads

  1. Replies: 7
    Last Post: 02-03-2012, 10:23 PM
  2. What are the Byte Streams in Java?
    By super soaker in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 06:26 AM
  3. Java I/O Streams
    By Dharamsi in forum Software Development
    Replies: 5
    Last Post: 15-01-2010, 02:57 PM
  4. Information on Windows Alternate Data Streams
    By DeMario in forum Operating Systems
    Replies: 5
    Last Post: 08-01-2010, 04:01 AM
  5. Wma streams - problem!
    By Maximus() in forum Windows Software
    Replies: 6
    Last Post: 31-12-2008, 02:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,407,633.64951 seconds with 17 queries