我想寫一個字節數組到文件,然後再讀一遍。問題是我讀的字節數組與我寫的不同。 下面的代碼的輸出是:從我寫的不同字節讀取
[B @ 21a06946(原始字節數組寫入)
[B @ 2fc14f68(字節陣列讀取)
byte[] encryptedKey = rsaCipher.encrypt(AESKey, publicKeyPathName, transformation, encoding);
System.out.println(encryptedKey);
List<byte[]> list = new ArrayList<byte[]>();
list.add(encryptedKey);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("encryptedKey"));
out.writeObject(list);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("encryptedKey"));
List<byte[]> byteList = (List<byte[]>) in.readObject();
in.close();
byte[] encryptedKey2 = byteList.get(0);
System.out.println(encryptedKey2);
實例不同,但是您檢查了內容嗎? – Tunaki
這不是你如何檢查數組是否相等,而是使用Arrays.equals(byte [] 1,byte [] 2)而不是 –
當你看到一個像[[B @ ...]這樣的字符串時,你應該注意到它是一個變量引用而不是其內容。 ''表示數組,''表示''字節''B','...處...「表示'... ...'。 –