【问题标题】:Different bytes read from which I wrote我从中读取的不同字节
【发布时间】:2015-10-19 17:10:53
【问题描述】:

我正在尝试将字节数组写入文件,然后再次读取。问题是我读的字节数组和我写的不同。 下面代码的输出是:

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

【问题讨论】:

  • 实例不同,但是你检查过内容吗?
  • 这不是您检查数组相等性的方式,请改用 Arrays.equals(byte[]1,byte[]2)
  • 当你看到像[B@... 这样的字符串时,你应该注意到它是一个变量引用而不是它的内容。 [ 代表数组,B 代表 byte@... 代表“at ...”。

标签: java byte inputstream outputstream


【解决方案1】:

数组没有正确的字符串表示。要查看内容,请改用以下内容

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

【讨论】:

  • 其实我可以自己考虑,非常感谢!
  • @user3376554 如果我的回答对你有帮助,你能接受吗? :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
相关资源
最近更新 更多