2015-10-19 70 views
0

我想寫一個字節數組到文件,然後再讀一遍。問題是我讀的字節數組與我寫的不同。 下面的代碼的輸出是:從我寫的不同字節讀取

[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); 
+4

實例不同,但是您檢查了內容嗎? – Tunaki

+3

這不是你如何檢查數組是否相等,而是使用Arrays.equals(byte [] 1,byte [] 2)而不是 –

+0

當你看到一個像[[B @ ...]這樣的字符串時,你應該注意到它是一個變量引用而不是其內容。 ''表示數組,''表示''字節''B','...處...「表示'... ...'。 –

回答

2

陣列不有一個正確的字符串表示。要查看內容,請使用下面的內容代替

System.out.println(java.util.Arrays.toString(encryptedKey)); 
System.out.println(java.util.Arrays.toString(encryptedKey2)); 
+0

其實我可以自己想想,非常感謝! – user3376554

+0

@ user3376554如果我的回答對你有幫助,你能接受嗎? :) –