java – 从ObjectInputStream读取不同的byte []而不是写入ObjectOutputStream
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 从ObjectInputStream读取不同的byte []而不是写入ObjectOutputStream,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2619字,纯文字阅读大概需要4分钟。
内容图文
![java – 从ObjectInputStream读取不同的byte []而不是写入ObjectOutputStream](/upload/InfoBanner/zyjiaocheng/722/b1bd041c7e7140ea85b898f81bbc41e2.jpg)
我对java有一个奇怪的问题.我想在ObjectOutputStream中写一个byte []并从那里写到一个新文件.该字节数组表示从磁盘读取的另一个文件.
之后,在写入新创建的文件后,我想从该文件中读取.但是现在从ObjectInputStream读取的byte []与写入的不同.
这就是我的问题:为什么这个字节[]不同?
为了清楚说明并让每个人都检查,我写了一个简短的程序,它将准确地表明我的意思:
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来节省一点空间.
内容总结
以上是互联网集市为您收集整理的java – 从ObjectInputStream读取不同的byte []而不是写入ObjectOutputStream全部内容,希望文章能够帮你解决java – 从ObjectInputStream读取不同的byte []而不是写入ObjectOutputStream所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。