我正在编写一个byte[]来归档和读取它。但是为什么写和读的byte[]是不同的呢?下面是我的代码:
import java.io.*;
public class test{
public static void main(String argv[]){
try{
byte[] Write = "1234567812345678".getBytes();
byte[] Read = new byte[16];
File test_file = new File("test_file");
test_file.mkdir();
String path = new String(test_file.getAbsolutePath()+"\\"+"test.txt");
File test_out = new File(path);
test_out.createNewFile();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(test_out));
out.write(Write);
out.close();
File test_in = new File(path);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(test_in));
while(in.available()>0)
in.read(Read);
in.close();
System.out.println(Write);
System.out.println(Read);
}catch ( Exception e ){
e.printStackTrace();
}
}
}这是输出;输入和输出是不同的:
[B@139a55
[B@1db9742发布于 2015-12-30 02:02:08
[B@139a55
[B@1db9742
这些是打印byte[]的输出--它是对象的哈希码。它与它的实际内容无关。
它只告诉您正在打印两个不同的对象-它们的内容可能仍然相同。
您应该打印字节数组的实际内容:What's the simplest way to print a Java array?
https://stackoverflow.com/questions/34516419
复制相似问题