对象ObjectInputStream的文件操作,报java.io.StreamCorruptedException异常。
我在给文件了写对象,每次写三个QQ对象,每次写得是追加在原来的后面的。写是正常的,但当我从文件中读对象时,只能读到第一次写得,就报下面的异常:
java.io.StreamCorruptedException
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
at test.main(test.java:24)
谁能帮我解决,加20分,好的话还可以多加。谢谢。
我的代码如下:
文件1:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import com.qq.server.QQ;
public class test2 {
public static void main(String[] args) {
FileOutputStream fos=null;
ObjectOutputStream oos=null;
try {
File file=new File("QQ1.txt");
fos=new FileOutputStream(file,true);
oos=new ObjectOutputStream(fos);
QQ qq=new QQ(1,"231","fds","fds","sadf",3213,"fdsaf","fdsafsd");
QQ qq1=new QQ(1,"231","fds","fds","sadf",3213,"fdsaf","fdsafsd");
QQ qq2=new QQ(1,"231","fds","fds","sadf",3213,"fdsaf","fdsafsd");
oos.writeObject(qq);
oos.writeObject(qq1);
oos.writeObject(qq2);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(oos!=null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件2:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import com.qq.server.QQ;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
File file = new File("QQ.txt");
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
QQ qq = null;
while ((qq = (QQ) ois.readObject()) != null) {
System.out.println("qq:" + qq);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
if(ois!=null){
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件3:
package com.qq.server;
public class QQ implements java.io.Serializable{
private int id;
private String nickname;
private String name;
private String passwd;
private String rpasswd;
private double tel;
private String mail;
private String Signed;
private static final long serialVersionUID = 123456789l;
public QQ(){
}
public QQ(int id, String nickname, String name, String passwd,String rpasswd, double tel, String mail, String signed) {
super();
this.id = id;
this.nickname = nickname;
this.name = name;
this.passwd = passwd;
this.rpasswd = rpasswd;
this.tel = tel;
this.mail = mail;
this.Signed = signed;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getSigned() {
return Signed;
}
public void setSigned(String signed) {
Signed = signed;
}
public double getTel() {
return tel;
}
public void setTel(double tel) {
this.tel = tel;
}
public String getRpasswd() {
return rpasswd;
}
public void setRpasswd(String rpasswd) {
this.rpasswd = rpasswd;
}
public boolean equals(Object obj){
if (obj instanceof QQ) {
QQ qq = (QQ) obj;
if(qq.getNickname().equals(this.getNickname())&&qq.getName().equals(this.getName())){
return true;
}
}
return false;
}
public int Hashcode(){
return nickname.hashCode()^name.hashCode();
}
public String toString(){
return id+","+nickname+","+name+","+passwd+","+tel+","+mail+","+Signed;
}
}
[解决办法]
在Test2.java中加入红色代码
第一部分是防止重复插入数据,第二部分是插入结束标志
File file = new File("QQ1.txt");
if(file.exists()){
file.delete();
}
fos = new FileOutputStream(file, true);
...................
...............
oos.writeObject(qq2);
oos.writeObject(null);
oos.flush();
[解决办法]
oos.writeObject(qq);
oos.writeObject(qq1);
oos.writeObject(qq2);
oos.flush();
把oos.flush();改成oos.reset();看看能不能解决问题。
估计你再次把对象写入文件的时候,用的是同一个对象(只是把对象里的成员信息修改)。这样的话程序是不会把对象修改后的版本写入文件的。
[解决办法]
public class test {
/**
* @param args
*/
public static void main(String[] args) {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
File file = new File("QQ.txt");
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
QQ qq = null;
while ((qq = (QQ) ois.readObject()) != null) {
System.out.println("qq:" + qq);
qq = (QQ) ois.readObject(); //添加这个,继续读啊!
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
if(ois!=null){
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[解决办法]
3楼的方法,试了一下
执行一次 test2.java 后,再执行 test.java 出现 java.io.EOFException
执行多次 test2.java 后,再执行 test.java 出现 java.io.StreamCorruptedException
4楼的方法试都不用试,肯定错
因为打印的只是奇数行数据
[解决办法]
ObjectOutputStream是不能append的(magic number问题),必须把以前的取出来重新再放一遍,不然读的时候会抛java.io.StreamCorruptedException
ObjectInputStream的readObject()方法也不会返回null,除非你写入null,他只会抛java.io.EOFException 异常
[解决办法]
package com.qq.server;
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.util.ArrayList;
import java.util.List;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Test test = new Test();
QQ qq = new QQ(1, "231", "fds", "fds", "sadf", 3213, "fdsaf", "fdsafsd");
System.out.println("新增的QQ :" + qq + "\n");
test.saveDataToFile("qq.txt", qq);
System.out.println("保存后的数据:");
List <QQ> list = test.getDataFromFile("qq.txt");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
/**
* 写数据
*
* @param filePath
* @param newQQ
*/
public void saveDataToFile(String filePath, QQ newQQ) {
List <QQ> qqs = getDataFromFile(filePath);
FileOutputStream fos = null;
ObjectOutputStream oos = null;
qqs.add(newQQ);
try {
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
fos = new FileOutputStream(file, false);
oos = new ObjectOutputStream(fos);
for (int i = 0; i < qqs.size(); i++) {
oos.writeObject(qqs.get(i));
}
oos.writeObject(null);
oos.flush();
oos.reset();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 读数据
*
* @param filePath
* @return
*/
public List <QQ> getDataFromFile(String filePath) {
List <QQ> qqs = new ArrayList <QQ>();
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
// 第一次执行时,因为没有数据,所以强行加入一个结束符号
FileOutputStream fos = new FileOutputStream(file, true);
fos = new FileOutputStream(file, true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(null);
oos.flush();
oos.close();
}
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
QQ qq = null;
while ((qq = (QQ) ois.readObject()) != null) {
qqs.add(qq);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return qqs;
}
}