简体   繁体   English

每个输入/输出后是否需要重新创建DataInputStream或DataOutputStream

[英]Do I need to recreate DataInputStream or DataOutputStream after each input/output

Hello so I was just wondering. 您好,我只是想知道。 I am creating multiplayer for my game and if I want to send something should I use: 我正在为游戏创建多人游戏,如果我想发送一些东西,我应该使用:

dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
dos.writeUTF(username);

dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
dos.writeUTF("test");

dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String loginResponse = dis.readUTF();

or should I use: 还是应该使用:

dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
dos.writeUTF(username);
dos.writeUTF("test");

dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String loginResponse = dis.readUTF();

You should create one IOStream and do with it what you need to do with it, without creating a new one every time. 您应该创建一个IOStream并对其进行处理,而不必每次都创建一个新的。

If you would create a new one everytime, theoretically you'd just be filling up memory unnecessarily. 如果您每次都创建一个新内存,那么从理论上讲,您只是在不必要地填充内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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