简体   繁体   中英

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.

If you would create a new one everytime, theoretically you'd just be filling up memory unnecessarily.

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