• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the main difference in c/c++ AND java input/output method? Why concept of STREAM in java?

 
Bhavesh Kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ALL , One person ask me that why streams were added to java.. ? what was the problem with simple input/output operation in C...? Please clarify the actual concept of streams in java.....

 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Streams provide a very important abstraction level. A lot of simple C operations require you to know something about the underlying source or sink. Streams provide you with a way to read or write data, without caring where it's coming from, or where it's going to.

Let's say you have an object, and you want to convert it to raw data so you can store it somewhere. Its class could have a method that accepts an OutputStream, and writes the data that represents the object to it.
This gives you two important advantages: First off, you can easily write data in different formats by wrapping streams with other streams that provide additional features. In the example I used a DataOutputStream to write integers and Strings to the output. In C, I would probably first have to convert data manually into some array of bytes.

The second advantage is, as I already mentioned, that the code that does the writing doesn't need to care about where the data is going to, or coming from. Client code can easily change the location by providing a different stream. You can provide the object with a stream to a file, or maybe a stream to a socket, if you want to store the data across a network. In C, you would probably have to write different functions for different outputs, or you would have to write a function that converts the entire structure to a raw array of bytes, which can be lumbering for big data types.

Now, I don't have much experience with C, so I imagine that there are ways that simplify this process a lot more than I wrote here; there probably is a library that provides streams to C. It should give you a good idea of why streams are awesome though.
 
Bhavesh Kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greatly thanks Mr.Stephan !

Now I'm sure and a lot confident on the answer. Thanks again...||
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!