对象ObjectInputStream的文件操作,报java.io.StreamCorruptedException异常。

mobojason 2008-08-07 12:18:24
我在给文件了写对象,每次写三个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;
}
}

...全文
1028 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
a1044303587 2011-10-18
  • 打赏
  • 举报
回复
建一个容器,将QQ对象保存在容器中,然后将此容器的对象写进去...用的时候读出之后遍历容器即可....
sd5816690 2008-08-07
  • 打赏
  • 举报
回复
3楼的方法,试了一下
执行一次 test2.java 后,再执行 test.java 出现 java.io.EOFException
执行多次 test2.java 后,再执行 test.java 出现 java.io.StreamCorruptedException



4楼的方法试都不用试,肯定错
因为打印的只是奇数行数据
xuhaiyang 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 sunxiong0 的回复:]
首先谢谢你!
你这样写,加上
if(file.exists()){
file.delete();
}
下一次写得时候就会把上一次写入的内容删掉,这样的话就不能追加了。去掉
if(file.exists()){
file.delete();
}
每次读的时候只能读到第一次写得。
有没有更好的方法。
[/Quote]
不是把原来文件删了,而是把你们的数据先读出来,然后再和新数据一起写回去,
具体可以参见楼上,要不要写入null看你读对象的方法怎么写了,不写的话度的时候就会抛java.io.EOFException异常,捕捉一下就可以了
sd5816690 2008-08-07
  • 打赏
  • 举报
回复
把读、写操作抽象为了两个方法
调用代码在 main 方法中


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;
}
}

sd5816690 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 sunxiong0 的回复:]
首先谢谢你!
你这样写,加上
if(file.exists()){
file.delete();
}
下一次写得时候就会把上一次写入的内容删掉,这样的话就不能追加了。去掉
if(file.exists()){
file.delete();
}
每次读的时候只能读到第一次写得。
有没有更好的方法。
[/Quote]
存的时候把原有数据取出来,和新数据组合后一起存
mobojason 2008-08-07
  • 打赏
  • 举报
回复
首先谢谢你!
你这样写,加上
if(file.exists()){
file.delete();
}
下一次写得时候就会把上一次写入的内容删掉,这样的话就不能追加了。去掉
if(file.exists()){
file.delete();
}
每次读的时候只能读到第一次写得。
有没有更好的方法。
mobojason 2008-08-07
  • 打赏
  • 举报
回复
声明一下,上面的文件1和文件2中的文件应该是同一个,不是原来的一个是QQ1.txt,一个是QQ.txt;
fwloveyou 2008-08-07
  • 打赏
  • 举报
回复
学习了,呵呵
dudenglan 2008-08-07
  • 打赏
  • 举报
回复
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();
}
}
}
xuhaiyang 2008-08-07
  • 打赏
  • 举报
回复
ObjectOutputStream是不能append的(magic number问题),必须把以前的取出来重新再放一遍,不然读的时候会抛java.io.StreamCorruptedException

ObjectInputStream的readObject()方法也不会返回null,除非你写入null,他只会抛java.io.EOFException 异常
cjphenry 2008-08-07
  • 打赏
  • 举报
回复
oos.writeObject(qq);
oos.writeObject(qq1);
oos.writeObject(qq2);
oos.flush();

把oos.flush();改成oos.reset();看看能不能解决问题。
估计你再次把对象写入文件的时候,用的是同一个对象(只是把对象里的成员信息修改)。这样的话程序是不会把对象修改后的版本写入文件的。
sd5816690 2008-08-07
  • 打赏
  • 举报
回复
java.io.StreamCorruptedException
是因为没有插入结束标志造成的
sd5816690 2008-08-07
  • 打赏
  • 举报
回复
在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();

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧