急急急!!!对象流问题,欢迎解答!! 我把一个对象数组输出到对象流文件中,因为我设置的输出对象流文件是可以追加输出的,所以然后我又把相同类型的对象数组再输出到同一个文件中。然后进行读取所有的对象,可是当我读取出一次对象之后,以后每次读取的都是同一个对象,根本无法读取到下一个对象。我的代码应该是没有问题的,可为什么每次读取的都是都是重复的对象?希望有人帮我回答,类似的问题我看过,可是没能看到解决方案!!!!!!急急急!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.Button;import java.awt.Frame;import java.awt.GridLayout;import java.awt.Label;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;public class Input extends Frame implements ActionListener { String a,b,c,d,e; Label l1; TextField tf1; Label l2; TextField tf2; Label l3; TextField tf3; Label l4; TextField tf4; Label l5; TextField tf5; Button bu; Input() { l1=new Label("楼号"); add(l1); tf1=new TextField(); add(tf1); l2=new Label("单元号"); add(l2); tf2=new TextField(); add(tf2); l3=new Label("门牌号"); add(l3); tf3=new TextField(); add(tf3); l4=new Label("业主姓名"); add(l4); tf4=new TextField(); add(tf4); l5=new Label("居住面积"); add(l5); tf5=new TextField(); add(tf5); bu=new Button("录入"); bu.addActionListener(this); add(bu); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } } ); setSize(500,600); setVisible(true); setLayout(new GridLayout(6,1)); pack(); } public void actionPerformed(ActionEvent act) { if(act.getSource()==bu) a=tf1.getText(); b=tf2.getText(); c=tf3.getText(); d=tf4.getText(); e=tf5.getText(); run(); } public void run() { int i=0; User u[]=new User[100]; try { File f = new File("C:/1.txt"); if(!f.exists()) { f.createNewFile(); } FileInputStream fis=new FileInputStream(f); ObjectInputStream ois = null; Object ooo = null; try{ ois=new ObjectInputStream(fis); ooo= ois.readObject(); }catch (Exception e) { // TODO: handle exception } while( ooo!=null) { u[i]=(User)ooo; i++; try{ ooo= ois.readObject(); }catch (Exception e) { break; } } if(ois!=null) { ois.close(); } fis.close(); User t1 = new User(a,b,c,d,e);; u[i] = t1; FileOutputStream fos=new FileOutputStream(f,false); ObjectOutputStream oos=new ObjectOutputStream(fos); for(int ii=0;ii<=i;ii++) { oos.writeObject(u[ii]); System.out.println(u[ii]); } oos.close(); fos.close(); } catch(Exception e) { e.printStackTrace(); System.out.println("出现异常"); } } public static void main(String[] args) { Input in=new Input(); } } class User implements Serializable { /** * */ private static final long serialVersionUID = -3700651775349081569L; String a1,b1,c1,d1,e1; User(String a2,String b2,String c2,String d2,String e2) { a1=a2; b1=b2; c1=c2; d1=d2; e1=e2; } public String toString() { String s="楼号"+a1+"单元号"+b1+"门牌号"+c1+"业主姓名"+d1+"面积"+e1; return s; } } 空文件时会产生EOF异常! 答:1)“我的代码应该是没有问题的”,楼主的代码肯定是有问题的。问题在何处:楼主知道对象流中是如何添加对象的吗?2)向对象流中添加对象,不仅仅只是:设置的输出对象流文件是可以追加输出的,就完事了的。 那如何写代码呢?原理是:从ObjectOutputStream继承,重写它的writeStreamHeader()方法,代码是:若是“添加方式”,则不用再向流中写入“头部数据”。注意:writeStreamHeader()方法是在对象的构造器中调用的,因此:根据对象的初始化顺序,必须在构造器执行之前或当时,就能告之是不是“添加方式”写对象。[我在下边示例代码中采用的是static 方法,楼主可按自己的要求设计]3)原理知道后,示例代码如下(仅供楼主参考)目的:“以添加方式向对象流中写入对象数组,并从流中读取出来,进行验证”import java.io.*;class Point implements java.io.Serializable{ private int x; private int y; public Point() {} public Point(int x,int y) {this.x=x;this.y=y;} public String toString() { return "["+x+","+y+"] "; }}class AppendObjectOutputStream extends ObjectOutputStream{ private static boolean append=false; public static void setAppend(boolean append){AppendObjectOutputStream.append=append;} protected AppendObjectOutputStream() throws IOException, SecurityException { super(); } public AppendObjectOutputStream(OutputStream out) throws IOException { super(out); } @Override protected void writeStreamHeader() throws IOException { // TODO 自动生成方法存根 if(!append) super.writeStreamHeader(); } }public class TestOOAppend { /** * @param args * @throws IOException * @throws Exception */ public static void main(String[] args) throws Exception { // TODO 自动生成方法存根 //第一次向流中以“非添加方式”写入若干对象数组, AppendObjectOutputStream.setAppend(false);//第一次写时使用 AppendObjectOutputStream oos=new AppendObjectOutputStream( new FileOutputStream("d:/t1.ser")); //写入对象数组 oos.writeObject(new Point[]{new Point(3,4),new Point(5,6)}); oos.flush(); oos.close();// 第二次级以后向流中以“添加方式”写入若于对象, AppendObjectOutputStream.setAppend(true);//第二次及以后向流中“添加对象”写时用。 oos=new AppendObjectOutputStream( new FileOutputStream("d:/t1.ser",true)); //“添加方式”写入对象数组 oos.writeObject(new Point[]{new Point(7,8),new Point(9,10)}); oos.flush(); oos.close(); System.out.println("向对象流中添加对象数组后,添加后流中内容是:"); ObjectInputStream ois=new ObjectInputStream(new FileInputStream("d:/t1.ser")); try{ while(true){//读取出所有的对象数组 Point[] pt=(Point[])ois.readObject(); System.out.println("对象数组中对象:"); for(Point p:pt) System.out.print(p); System.out.println(); } }catch(EOFException e) { }finally{ ois.close(); } }}程序运行结果:向对象流中添加对象数组后,添加后流中内容是:对象数组中对象:[3,4] [5,6] 对象数组中对象:[7,8] [9,10] ConcurrentHashMap和ConcurrentSkipListMap类如何做线程安全循环 有没有什么配置在调用一个类方法的同时调用另一个方法 双核电脑与线程问题? 如何使用runtime去执行指定的java源文件??? hashmap加上synchonize与hashtable有啥区别 求助?? 在线等 关于Swing相当有难度的问题?? 帮我看看这段代码,很奇怪啊 一个让我很迷茫的问题 刚学JAVA,问个问题 我的sayhello程序哪错了 Hibernate 链接MSSQL出现错误了。
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Input extends Frame implements ActionListener
{
String a,b,c,d,e;
Label l1;
TextField tf1;
Label l2;
TextField tf2;
Label l3;
TextField tf3;
Label l4;
TextField tf4;
Label l5;
TextField tf5;
Button bu;
Input()
{
l1=new Label("楼号");
add(l1);
tf1=new TextField();
add(tf1);
l2=new Label("单元号");
add(l2);
tf2=new TextField();
add(tf2);
l3=new Label("门牌号");
add(l3);
tf3=new TextField();
add(tf3);
l4=new Label("业主姓名");
add(l4);
tf4=new TextField();
add(tf4);
l5=new Label("居住面积");
add(l5);
tf5=new TextField();
add(tf5);
bu=new Button("录入");
bu.addActionListener(this);
add(bu);
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
}
);
setSize(500,600);
setVisible(true);
setLayout(new GridLayout(6,1));
pack();
}
public void actionPerformed(ActionEvent act)
{
if(act.getSource()==bu)
a=tf1.getText();
b=tf2.getText();
c=tf3.getText();
d=tf4.getText();
e=tf5.getText();
run();
}
public void run()
{
int i=0;
User u[]=new User[100];
try
{
File f = new File("C:/1.txt");
if(!f.exists())
{
f.createNewFile();
}
FileInputStream fis=new FileInputStream(f);
ObjectInputStream ois = null;
Object ooo = null;
try{
ois=new ObjectInputStream(fis);
ooo= ois.readObject();
}catch (Exception e) {
// TODO: handle exception
}
while( ooo!=null)
{
u[i]=(User)ooo;
i++;
try{
ooo= ois.readObject();
}catch (Exception e) {
break;
}
}
if(ois!=null)
{
ois.close();
}
fis.close();
User t1 = new User(a,b,c,d,e);;
u[i] = t1;
FileOutputStream fos=new FileOutputStream(f,false);
ObjectOutputStream oos=new ObjectOutputStream(fos);
for(int ii=0;ii<=i;ii++)
{
oos.writeObject(u[ii]);
System.out.println(u[ii]);
}
oos.close();
fos.close();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("出现异常");
}
}
public static void main(String[] args)
{
Input in=new Input();
}
}
class User implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -3700651775349081569L;
String a1,b1,c1,d1,e1;
User(String a2,String b2,String c2,String d2,String e2)
{
a1=a2;
b1=b2;
c1=c2;
d1=d2;
e1=e2;
}
public String toString()
{
String s="楼号"+a1+"单元号"+b1+"门牌号"+c1+"业主姓名"+d1+"面积"+e1;
return s;
}
}
空文件时会产生EOF异常!
1)“我的代码应该是没有问题的”,楼主的代码肯定是有问题的。问题在何处:楼主知道对象流中是如何添加对象的吗?
2)向对象流中添加对象,不仅仅只是:设置的输出对象流文件是可以追加输出的,就完事了的。 那如何写代码呢?
原理是:从ObjectOutputStream继承,重写它的writeStreamHeader()方法,代码是:若是“添加方式”,则不用再向流中写入“头部数据”。
注意:writeStreamHeader()方法是在对象的构造器中调用的,因此:根据对象的初始化顺序,必须在构造器执行之前或当时,就能告之是不是“添加方式”写对象。[我在下边示例代码中采用的是static 方法,楼主可按自己的要求设计]
3)原理知道后,示例代码如下(仅供楼主参考)
目的:“以添加方式向对象流中写入对象数组,并从流中读取出来,进行验证”import java.io.*;class Point implements java.io.Serializable
{
private int x;
private int y;
public Point()
{}
public Point(int x,int y)
{this.x=x;this.y=y;}
public String toString()
{
return "["+x+","+y+"] ";
}
}class AppendObjectOutputStream extends ObjectOutputStream
{
private static boolean append=false;
public static void setAppend(boolean append){AppendObjectOutputStream.append=append;}
protected AppendObjectOutputStream() throws IOException, SecurityException {
super();
} public AppendObjectOutputStream(OutputStream out) throws IOException {
super(out);
}
@Override
protected void writeStreamHeader() throws IOException {
// TODO 自动生成方法存根
if(!append) super.writeStreamHeader();
}
}public class TestOOAppend { /**
* @param args
* @throws IOException
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO 自动生成方法存根
//第一次向流中以“非添加方式”写入若干对象数组,
AppendObjectOutputStream.setAppend(false);//第一次写时使用
AppendObjectOutputStream oos=new AppendObjectOutputStream(
new FileOutputStream("d:/t1.ser"));
//写入对象数组
oos.writeObject(new Point[]{new Point(3,4),new Point(5,6)});
oos.flush();
oos.close();
// 第二次级以后向流中以“添加方式”写入若于对象,
AppendObjectOutputStream.setAppend(true);//第二次及以后向流中“添加对象”写时用。
oos=new AppendObjectOutputStream(
new FileOutputStream("d:/t1.ser",true));
//“添加方式”写入对象数组
oos.writeObject(new Point[]{new Point(7,8),new Point(9,10)});
oos.flush();
oos.close();
System.out.println("向对象流中添加对象数组后,添加后流中内容是:");
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("d:/t1.ser"));
try{
while(true){//读取出所有的对象数组
Point[] pt=(Point[])ois.readObject();
System.out.println("对象数组中对象:");
for(Point p:pt)
System.out.print(p);
System.out.println();
}
}catch(EOFException e)
{
}finally{
ois.close();
}
}}
程序运行结果:
向对象流中添加对象数组后,添加后流中内容是:
对象数组中对象:
[3,4] [5,6]
对象数组中对象:
[7,8] [9,10]