稍后在Java中修改ByteArrayOutputStream .write值

时间:2018-06-24 18:54:01

标签: java

有什么方法可以稍后在代码中更改ByteArrayOutputStream .write的值吗? (请注意以下代码中的注释)

类似的东西:

public class test {
    public static int val;
    public static void main(String args[]) {

        ByteArrayOutputStream outgoingStream = new ByteArrayOutputStream();
        outgoingStream.write(1);
        outgoingStream.write(92);
        val = 23;
        outgoingStream.write(val);
        outgoingStream.write(34);
        val = 69;


        byte[] bytes = outgoingStream.toByteArray();

        ByteArrayInputStream in = new ByteArrayInputStream(bytes);

        for(int i = 0; i<bytes .length;i++) {
            //Outputs 1 92 23 34 but I need it as 1 92 69 34
            System.out.println(in.read());
        }   
    }

}

1 个答案:

答案 0 :(得分:-2)

您应该使用其他版本的写入方法和写入字节的地址起点,然后替换:

public void write(byte []b, int of, int len)
Writes len number of bytes starting from offset off to the stream.