java 打印byte数组内容,如何在Java中将byte []数组中的原始字节内容打印到stdout?...

I am doing the same project as describe here:

My problem is that when I try to print out bytes, I get weird results. My problems occur in the following code(Sorry for my bad choice of variables):

for(int k = 0; k < head.length; k++){

System.out.write(head[k]);

}

for(int m = 0; m < a.size(); m++){

int comprlength = a.get(m).getclength();

for(int ii = 0; ii < comprlength; ii++){

System.out.write(a.get(m).getcompr()[ii]);

}

}

for(int j = 0; j < f1.length; j++){

System.out.write(f1[j]);

}

for(int ll = 0; ll < total_d.length; ll++){

System.out.write(total_d[ll]);

}

The last two for-loops do not print out the contents of the their byte arrays. Thus I get a unexpected end of file error when using gzip. The weird thing is that if I comment out the second for-loop block (the block with the variables m and ii), nothing gets printed out.

So how do I properly print out the contents of my byte arrays? Why does the first for-loop print out properly when the second for-loop is not commented and why does it not print anything if that second for-loop is commented?

EDIT:

To be more specific:

I want to write out the raw bytes. And I want to do it so that it is right after each other for every one of my byte arrays

解决方案

Assuming your byte array is called buf:

System.out.println(Arrays.toString(buf));

Edit:

It sounds like what you really want to do is write your bytes to stdout, not print them. See http://docs.oracle.com/javase/6/docs/api/java/io/PrintStream.html for the difference between printing to a stream and writing to it. Easiest way should be to call the write(byte[] b) method:

System.out.write(buf);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将`sys.stdout`的内容通过WebSocket显示在网页,你可以按照以下步骤进行操作: 1. 首先,安装`websockets`库,可以使用以下命令进行安装: ``` pip install websockets ``` 2. 创建一个WebSocket服务器,可以使用下面的示例代码: ```python import asyncio import websockets import sys async def handle_websocket(websocket, path): while True: # 从sys.stdout读取内容 output = sys.stdout.getvalue() if output: # 发送内容到WebSocket客户端 await websocket.send(output) # 清空sys.stdout sys.stdout.truncate(0) await asyncio.sleep(0.1) start_server = websockets.serve(handle_websocket, 'localhost', 8765) asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_forever() ``` 这个服务器会在本地的8765端口上监听WebSocket连接,并将`sys.stdout`内容发送给客户端。 3. 在你的网页使用JavaScript代码连接到WebSocket服务器,并接收内容。你可以使用以下示例代码: ```javascript var socket = new WebSocket('ws://localhost:8765/'); socket.onmessage = function(event) { var output = event.data; // 将内容显示在网页的合适位置 console.log(output); }; ``` 这段代码会连接到本地的WebSocket服务器,并在接收到消息时将内容输出到浏览器的控制台。 通过这种方式,你可以将`sys.stdout`的内容通过WebSocket实时显示在网页。请确保在运行代码之前已经将`sys.stdout`重定向到`StringIO`对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值