我对Java有一个奇怪的问题。我想在ObjectOutputStream中以及从那里到新文件中(等)编写一个字节[]。该字节阵列表示从磁盘读取的其他文件。
稍后,写入新创建的文件后,我想从该文件中阅读。但是现在从ObjectInputStream读取的字节[]与书面的字节不同。
那是我的问题:为什么字节[]与众不同?
为了清楚起见,我要检查一个简短的程序,这将表明我的意思:
import java.io.*;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.security.MessageDigest;
import org.bouncycastle.util.encoders.Hex;
public class MyTest {
public static void main(String[] args) throws Exception {
// 1st step:
// ------------------------------------------------
byte[] data = openFile();
// Create file to write
FileOutputStream fos = new FileOutputStream(new File("test"));
ObjectOutputStream oosf = new ObjectOutputStream(fos);
// Write byte[]-length and byte[]
oosf.writeInt(data.length);
oosf.write(data);
// Flush & Close
fos.flush();
fos.close();
// Print hash value of saved byte[]
try {
final MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
messageDigest.reset();
System.out.println(new String(Hex.encode(messageDigest.digest(data))));
} catch (Exception e) {
}
// 2nd step
// ------------------------------------------------
// Open just saved file
FileInputStream fis = new FileInputStream(new File("test"));
ObjectInputStream ois = new ObjectInputStream(fis);
// Read the length and create a byte[]
int length = ois.readInt();
byte[] dataRead = new byte[length];
// Read the byte[] itself
ois.read(dataRead);
// Print hash value of read byte[]
try {
final MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
messageDigest.reset();
System.out.println(new String(Hex.encode(messageDigest.digest(dataRead))));
} catch (Exception e) {
}
// Both printed hash values should be the same
}
private static byte[] openFile() throws Exception {
// Download a sample file which will be converted to a byte[]
URL website = new URL("http://www.marcel-carle.de/assets/Cryptonify/Cryptonify-1.7.8.zip");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos2 = new FileOutputStream("tmp");
fos2.getChannel().transferFrom(rbc, 0, 1 << 24);
fos2.flush();
fos2.close();
// Open downloaded file and convert to byte[]
File selectedFile = new File("tmp");
FileInputStream fis1 = new FileInputStream(selectedFile);
byte[] data = new byte[(int) selectedFile.length()];
fis1.read(data);
fis1.close();
return data;
}
}
希望您能帮助我!
您忽略了异常;您没有关闭正确的流;而且您假设read()
填充缓冲区。使用readFully()
。您没有编写对象,因此您不妨将DataInputStream
和DataOutputStream
使用并节省一些空间。