多次创建ObjectOutputStream对象写对象的话,会重复写入header,导致读出时,出现streamcorrput异常,所以这里要判断是不是第一次写文件,若是则写入头部,否则不写入。
多次写对象的文件内容:
t str0t str1t str2 t str0t str1t str2
出现两个header信息( |),在读出时候就会报错:java.io.StreamCorruptedException: invalid type code: AC
示例:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectOldWriter {
public static void main(String[] args) {
String filename = "d://mydata.data";
oldWriter(filename);
oldWriter(filename);
oldReader(filename, 6);
}
public static void oldWriter(String filename) {
File file = null;
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
file = new File(filename);
fos = new FileOutputStream(file, true);
oos = new ObjectOutputStream(fos);
for(int i=0;i<3;i++){
oos.writeObject("str"+i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fos!=null) fos.close();
if(oos!=null) oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void oldReader(String filename,int count) {
FileInputStream fis = null;
ObjectInputStream ois = null;
try{
fis = new FileInputStream(filename);
ois = new ObjectInputStream(fis);
for(int i=0;i
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
try {
if(fis!=null) fis.close();
if(ois!=null) ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行结果(报错):
str0
str1
str2
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at com.effective.java.ObjectOldWriter.oldReader(ObjectOldWriter.java:51)
at com.effective.java.ObjectOldWriter.main(ObjectOldWriter.java:16)
解决方法:
1.用同一个ObjectOutputStream写对象
2.但是大部分时候,要不断往某个文件记录对象,这样按照1的说法就要维护一个ObjectOutputStream,但是重启应用时候就会重新创建一个ObjectOutputStream对象,此时如果还是想往刚才那个文件里写对象的话,就会追加一个header。这样在读对象时读到这个位置就会报错。
解决方法是重写ObjectOutputStream的writeStreamHeader()方法
示例:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
public class ObjectNewWriter {
ObjectNewWriter(){
}
public static void main(String[] args) {
String filename = "d://mydata.data";
newWriter(filename);
newWriter(filename);
newReader(filename, 6);
}
public static void newWriter(String filename) {
File file = null;
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
file = new File(filename);
fos = new FileOutputStream(file, true);
if(file.length()<1){
oos = new ObjectOutputStream(fos);
}else{
oos = new MyObjectOutputStream(fos);
}
for(int i=0;i<3;i++){
oos.writeObject("str"+i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fos!=null) fos.close();
if(oos!=null) oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void newReader(String filename,int count) {
FileInputStream fis = null;
ObjectInputStream ois = null;
try{
fis = new FileInputStream(filename);
ois = new ObjectInputStream(fis);
for(int i=0;i
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
try {
if(fis!=null) fis.close();
if(ois!=null) ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class MyObjectOutputStream extends ObjectOutputStream{
public MyObjectOutputStream() throws IOException {
super();
}
public MyObjectOutputStream(OutputStream out) throws IOException {
super(out);
}
@Override
protected void writeStreamHeader() throws IOException {
return;
}
}
记住要删除你D盘下的mydata.data再执行该类
运行结果:
str0
str1
str2
str0
str1
str2