简体   繁体   中英

What's the different between DataOutputStream write( int i ) and writeByte( int i )

If i want to send a integer through the socket, which should I use? What's the difference?

Socket socket=new Socket(localhost, 9876);
DataOutputStream writer=new DataOutputStream(socket.getOutputStream());
int i =  10;
writer.write(i);

writer.writeByte(i);

Do I need to add "\\r\\n" at the end when using writeByte?

The functionality is the same, they both write the eight low-level bits of the argument onto the underlying stream.

BUT writeByte is final and unsynchronized while write is not final and is synchronized

Moreover, DataOutputStream implements the DataOutput interface which contains the writeByte() and write() method and overrides write() declared in its superclass FilterOutputStream. That's the reason why DataOutputStream ends up having two different methods doing the same job...

Also Refer to Java Doc

There is no difference. They do the same thing. DataOutputStream needs to implement write(int) because it's part of the OutputStream type.

Neither of them write any line terminators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM