System arraycopy和追加文件



我有一个问题。

是否有能力从这些创建一个8字节的文件txt ?

byte[] bk1;
bk1=readBytesFromFile("Z:\secretkey.txt"); // contain - aa
byte[] bk;
bk=readBytesFromFile("Z:\wIV.txt"); // contain - 123456789

byte[] out = new byte [bk.length + bk1.length];
System.arraycopy(bk, 2, out, 4, bk1.length);
//out.length = 8; - not working
saveBytesToFile("Z:\IVsk.txt", out);

我想把bk和bk1联系起来。打开bk,粘贴bk1的内容在2 - 4的位置-像这样:12aa456712aa3456-我需要这个文件8字节,并将其保存到IVsk.txt

听起来你只是很难弄清楚传递给System::arraycopy的参数。您可以随时查阅api了解更多细节。话虽如此,这里有一个快速的用例示例,其中数组ab合并在一起。

// Inputs to merge
byte[] a = "abcdefghij".getBytes();
byte[] b = "0123456789".getBytes();
// Output array
byte[] out = new byte [8];

可以先用第一个数组填充,然后替换第二个数组中的特定元素,从而使两个数组重叠。

// Fill out with the first few elements of a
System.arraycopy(a, 0, out, 0, out.length);
// Out now has value: "abcdefgh"
// Replace 2 elements starting from index 2 of out with bk 
System.arraycopy(b, 0, out, 2, 2);
// Out now has value: "ab01efgh"

根据你的问题,你也可以做一个额外的复制,以确保没有元素被覆盖。

// Fill out with the first few elements of a
System.arraycopy(a, 0, out, 0, 2);
// Out now has value: "ab------" ('-' represents 0 in array)
// Replace 2 elements starting from index 2 of out with b 
System.arraycopy(b, 0, out, 2, 2);
// Out now has value: "ab01----" ('-' represents 0 in array)
// Fill remainder with remaining elements of a 
System.arraycopy(a, 2, out, 4, 4);
// Out now has value: "ab01cdef"

最新更新