如何读入和写入呢?帮忙看下代码哪里错了.Serializable问题.io问题,还是单立模式问题?

happyday4832 2005-11-06 12:11:41
我做的是一个包含图形信息的文件,目的是能保存,再读出来.(在GraghView Serialiable了.)
可是现在输入输出都没有异常,能保存成一个40k左右的文件,读不出来.
我在想是不是因为GraghView里面的getInstance是单立模式,所以不能初始化呢?/或者io写的有问题?
//输出
private void saveFile() {
JFileChooser chooser = new JFileChooser();
int state = chooser.showSaveDialog(MainFrame.this);
GraghView s= new GraghView();
File f = chooser.getSelectedFile();
try {
ObjectOutputStream out =
new ObjectOutputStream(new FileOutputStream(f,true));
out.writeObject(s);
out.flush();
out.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
//输入
private void openFile() {
JFileChooser chooser = new JFileChooser();
int state = chooser.showOpenDialog(MainFrame.this);
GraghView s= new GraghView();
File f = chooser.getSelectedFile();
try {
ObjectInputStream in =
new ObjectInputStream(new FileInputStream(f));
s =(GraghView)in.readObject();

in.close();
} catch(Exception e) {
e.printStackTrace();
}



}
}
//
...全文
161 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
happyday4832 2005-11-23
  • 打赏
  • 举报
回复
我想是读出来,但显示不出来的问题.于是加了一段:
GraghNode node=null;
node.getgraghnodelayoutManager().add(node);//从Box中将节点加入
node.getgraghnodelayoutManager().add(node.theContainer);
node.getTheContainer().add(node.getgraghnodelayoutManager());
GraghView.getInstance().draw();//重画
GraghView.getInstance().revalidate();//刷新

不过不行,java.lang.NullPointerException报错,说node.getgraghnodelayoutManager().add(node);等几句有问题.
怎么回事呢?
happyday4832 2005-11-23
  • 打赏
  • 举报
回复
import java.io.Serializable;
public class GraghView extends JPanel implements MouseListener,MouseMotionListener,Serializable

public class Gragh implements Serializable

implements Serializable不是序列化的方法吗?

现在问题有了新进展,报错解决了,(是多写了一个in.readObject();//blush)
现在的焦点成了如何将这个文件正确显示出来.~~
GraghView.getInstance().revalidate();也不好使~~



kingofhawks 2005-11-22
  • 打赏
  • 举报
回复
搂主应该是要把Gragh的数据序列化是吧,可是在你的代码中没有对它进行序列化的操作阿。
kingofhawks 2005-11-22
  • 打赏
  • 举报
回复
哦,你的GraghView类没有实现序列化操作阿?writeObject,readObject呢?
kingofhawks 2005-11-22
  • 打赏
  • 举报
回复
现在用 GraghView view = null;
view=(GraghView)in.readObject();
替换上面说的问题,结果是java.io.EOFException
读文件的时候出错,能不能把你的GraghView的序列化代码贴出来看一下,可能是序列化那个类的对象时有问题导致你读不出来啊.
happyday4832 2005-11-22
  • 打赏
  • 举报
回复
现在用 GraghView view = null;
view=(GraghView)in.readObject();
替换上面说的问题,结果是java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte;(ObjectInputStream.java:2435)
at com.zwb.wsc.frames.MainFrame.openFile(MainFrame.java:395)//本段程序就是MainFrame;at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
主要这几部分的错误。

kingofhawks(蓝鹰):我没明白你的意思,是原搬这段程序就可以吗?我试验了System.out.println("class-----"+in.readObject().getClass()),结果是报错----java.io.EOFException,输出不了。

blddp() :我把那句去掉了.

autowind(一个人住真痛苦) :你说静态的field无法实现serizlization,是说单立模式无法serialization?

单立GraghView:
package com.zwb.wsc.gragh;

import java.awt.Color;//很多import 略写;

//用于显示Gragh的面板

public class GraghView extends JPanel implements MouseListener,MouseMotionListener,Serializable
{
public Gragh gragh;
public static GraghView instance = new GraghView();
public Box layoutManager ;

private GraghView()
{
super();
this.layoutManager = Box.createVerticalBox();
this.add(layoutManager);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setBackground(Color.white);
this.gragh = GraghFactory.getGraghInstance();
this.setSize(new Dimension(1000,1000));
}

public static GraghView getInstance()
{
if(instance == null)
{
instance = new GraghView();
}
return instance;
}

public static void setInstance(GraghView graghview)
{

instance = graghview;
//return instance;

}
public void draw()
{
GraghNode node = this.gragh.getStartNode();//得到开始节点
while(true)
{
node.draw(this.layoutManager);
System.out.println("i have drawn the node");
if(node.getChild()!=null)
{
System.out.println("not null");
node = (GraghNode)node.getChild();
System.out.println("get the child node");
}
else
{
System.out.println("the list is finished");
break;

}
}

}

public Gragh getGragh() {
return gragh;
}


public void setGragh(Gragh gragh) {
this.gragh = gragh;
}

贴了主要的代码。谢谢继续关注。
autowind 2005-11-21
  • 打赏
  • 举报
回复
静态的field无法serialization.
blddp 2005-11-21
  • 打赏
  • 举报
回复
代码看不出问题,不过我看到了out.flush();
ObjectOutputStream
ObjectInputStream
并非BufferedStream,所以flush()是没有用的,仅仅是空实现
kingofhawks 2005-11-21
  • 打赏
  • 举报
回复
System.out.println("class-----"+in.readObject().getClass())打出来看看呢,还有把你的单子模式也贴出来瞧瞧~
happyday4832 2005-11-21
  • 打赏
  • 举报
回复
我查到一个问题,
GraghView是单立模式,所以只能有一个对象,
GraghView s= new GraghView();是错的.
但是,换成
Object a=null;
a=(GraghView)in.readObject();
仍不可以,a 仍染是Object类型,很奇怪.
怎么回事呢?
crazycy 2005-11-19
  • 打赏
  • 举报
回复
感觉没有问题,关注学习中
weastkon 2005-11-19
  • 打赏
  • 举报
回复
这个代码的Serialize操作应该是没问题的,问题可能是GraghView吧
紫炎圣骑 2005-11-08
  • 打赏
  • 举报
回复
学习。。。
火山企鹅 2005-11-08
  • 打赏
  • 举报
回复
光看你这个代码还真看不出什么问题,只能看看你其他代码了。

62,623

社区成员

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

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