-1

Why DataInputStream/DataOutputStream is used or preferred over any other stream of java like BufferedInputStream/BufferedOutputStream , BufferedReader/BufferedWriter in Socket programming like Server Client program ?

Please explain the difference between them ??

Give the detailed feature of DataOutputStream (like what it does).

And explain explicitly why & when do we need DataInputStream/DataOutputStream ??

Thanks in Advance.

Tom
  • 41
  • 9
  • @Axel, I did not ask for "do-my-homework-for-me" , I didn't find the detailed information about DataOutputStream , so I asked it . And I know that some problem occurred when you use "BufferedInputStream/BufferedOutputStream , BufferedReader/BufferedWriter " , but the problem solved after using DataOutputStream , but I don't know the underlying reason . please don't vote to close . – Tom Jun 08 '17 at 09:01
  • About the differences between them, have you read their documentation? – RealSkeptic Jun 08 '17 at 09:02
  • @Axel I don't see any homework here :3 – niceman Jun 08 '17 at 09:06
  • 1
    @niceman , Thanks for supporting and understanding . – Tom Jun 08 '17 at 09:31
  • @RealSkeptic , I didn't find what I'm looking for . – Tom Jun 08 '17 at 09:32
  • why doesn't the below answer answer your question ? – niceman Jun 08 '17 at 09:35
  • Then it is not clear what you're looking for. Please [edit] your question and explain how you got the impression that `DataInputStream` solves a problem with the other types of input/output when using sockets. What problem exactly was tackled? – RealSkeptic Jun 08 '17 at 09:35
  • @freedev , I don't know how you find my tone was rough , cause I know I was polite . But if you find it was not polite , then I'm sorry , this is my first question in StackOverflow , I'm totally new . – Tom Jun 08 '17 at 09:35
  • @niceman , He didn't answer all of my question in detail . – Tom Jun 08 '17 at 10:44
  • @RealSkeptic , I was using BufferedReader/BufferedWriter , but can't send text typed in keyboard from server to client and vice-versa . But When I used DataInputStream/DataOutputStream , the problem was solved , I want to know the exact underlying reason . BufferedReader/BufferedWriter is Character stream , is this the reason for the problem ?? – Tom Jun 08 '17 at 10:48
  • Please [edit] your question, and add the exact circumstances of the problem you had. **Not in the comments**, in the question. Show your code before the correction and after the correction. Explain what the problem was, and maybe we'll be able to answer you. You seem to think that this is something everybody knows - but it isn't. It's your particular project. So give the exact details **in the question**. – RealSkeptic Jun 08 '17 at 10:51
  • @Tom as RealSkeptic said you should add those details in the future, the answer below and my comment are examples of the misunderstanding we had when we read your question, I thought that you actually don't know what `Data(Input,Output)Stream` is – niceman Jun 08 '17 at 12:17
  • @RealSkeptic , I solved my problem , actually both DataInputStream/DataOutputStream and BufferedReader/BufferedWriter can be used , thanks for helping . – Tom Jun 08 '17 at 13:02
  • @niceman , I actually didn't know a lot about DataInputStream/DataOutputStream and what is a primitive data types , Thanks to Roman Puchkovskiy , I understand now . I have all the answers , thanks to you too. – Tom Jun 08 '17 at 13:05
  • @Tom I retracted my close vote. It looked like homework because your question was quite unspecific and not a single line of code was shown. Please take the time to read the "How to ask" documentation. – Axel Jun 08 '17 at 13:23
  • @Axel , thank you for retracting , and I'm new here , just Sign up today , needed to clear my doubts urgently . Next time , I'll read the "How to ask" documentation carefully. anyway I got -2 votes , is it bad , what should I do ? – Tom Jun 08 '17 at 14:05
  • @Tom If you have a problem like this, describe what you have already tried and post the code that you have problems with. Also post the error messages and/or stack trace. Don't worry too much about the downvotes. I think your next question will receive upvotes. – Axel Jun 09 '17 at 07:20
  • @Axel , I got all the answers , my problem is solved . And thank you for guiding me . – Tom Jun 10 '17 at 07:27

2 Answers2

1

I got all the answer , thanks to Roman Puchkovskiy . And I also found that DataInputStream/DataOutputStream and BufferedReader/BufferedWriter both can be used in Socket programming , but using DataInputStream/DataOutputStream is preferred , cause

" It is very convenient if you have to read/write data in some primitives (short/int/utf8 string/...) and not just bytes. " as Roman Puchkovskiy said.

And thanks to you all of you for coming forward to help me.

Tom
  • 41
  • 9
0

DataInputStream/DataOutputStream implement DataInput/DataOutput interfaces and hence have methods like writeShort(), readInt() and so on. It is very convenient if you have to read/write data in some primitives (short/int/utf8 string/...) and not just bytes.

On the sending side, you do something like

OutputStream os = ... // let's say you already have it
DataOutputStream dos = new DataOutputSream();
dos.writeInt(42);

In the receiving side, it's

InputStream is = ... // let's say you already have it
DataInputStream dis = new DataInputStream(is);
int intValue = dis.readInt();

Now intValue is 42. You don't have to think about int to bytes coversion, byte order and so on, you just write and then read it with convenient methods.

Roman Puchkovskiy
  • 11,415
  • 5
  • 36
  • 72
  • Please tell me the difference between usage of them on perspective of socket programming cause I'm having problem with BufferedReader/BufferedWriter when exchanging text typed in keyboard . Sorry for bad English . – Tom Jun 08 '17 at 10:44