簡體   English   中英

稍后在Java中修改ByteArrayOutputStream .write值

[英]Modify ByteArrayOutputStream .write value later in 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());
        }   
    }

}

您應該使用另一種版本的write方法和寫入字節的地址起點,然后替換:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM