java写入数组_如何在Java中将数组写入outputStream
我想通過Socket發送多個隨機值.我認為數組是發送它們的最佳方式.但是我不知道如何將數組寫入Socket outputStream?
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.io.*;
import java.util.Random;
class NodeCommunicator {
public static void main(String[] args) {
try {
Socket nodejs = new Socket("localhost",8181);
Random randomGenerator = new Random();
for (int idx = 1; idx <= 1000; ++idx){
Thread.sleep(500);
int randomInt = randomGenerator.nextInt(35);
sendMessage(nodejs,randomInt + " ");
System.out.println(randomInt);
}
while(true){
Thread.sleep(1000);
}
} catch (Exception e) {
System.out.println("Connection terminated..Closing Java Client");
System.out.println("Error :- "+e);
}
}
public static void sendMessage(Socket s,String message) throws IOException {
s.getOutputStream().write(message.getBytes("UTF-8"));
s.getOutputStream().flush();
}
}
總結
以上是生活随笔為你收集整理的java写入数组_如何在Java中将数组写入outputStream的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python队列精灵对战_python队
- 下一篇: linux 镜像错误,VituralBo