1.如下代码,将文件2.log序列化后保存到2.txt
FileInputStream fin = new FileInputStream("D:\\2.log");
FileOutputStream out1 = new FileOutputStream(new File("D:\\2.txt"));
// ObjectOutputStream oop1 = new ObjectOutputStream(out1);
byte[] datas = new byte[1024];
int j = 0;
while ((j = fin.read(datas)) > 0) {
out1.write(datas);
fin.read(datas);
}
fin.close();
out1.close();
反序列化:
使用ObjectInputStream()
FileInputStream fis = new FileInputStream("D:/2.txt");
ObjectInputStream oin = new ObjectInputStream(fis);
oin.readObject();
报错:Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 3139322E
该怎么处理?
如果文件较大,读入时加入缓存该怎么写?望大牛赐教,xiexie