如何在Java中将byte []数组中的原始字节内容打印到stdout? [英] How to print raw byte content from a byte[] array to stdout in Java?
问题描述
我正在做与此处描述的项目相同的项目:
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]);
}
最后两个for循环不会打印出它们的字节内容阵列。因此,使用gzip时出现意外的文件错误。奇怪的是,如果我注释掉第二个for-loop块(带有变量m和ii的块),则不会打印出任何内容。
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.
那我该如何正确打印出我的字节数组的内容?为什么第二个for循环没有被注释时第一个for-loop打印正确,如果第二个for循环被注释,为什么它不打印?
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?
编辑:
更具体一点:
我想写出原始字节。我想这样做,以便我的每个字节数组都在彼此之后
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
推荐答案
假设你的字节数组被称为 buf :
Assuming your byte array is called buf:
System.out.println(Arrays.toString(buf));
编辑:
听起来你真正想做的就是把你的字节写到stdout ,不要打印它们。请参阅 http://docs.oracle.com/javase/ 6 / docs / api / java / io / PrintStream.html 了解打印到流和写入流之间的区别。最简单的方法应该是调用write(byte [] b)方法:
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);
这篇关于如何在Java中将byte []数组中的原始字节内容打印到stdout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!