如何将一个结构保存文件 写了一个程序,里面的对象间是树型关系,想保存这棵树到文件,有什么简便的方法?如何从文件中读回?如何保存程序现场到一个文件中?又如何读取?多多指教! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 同意楼上如果实现了serializable,直接写入也可以 问题是我know nothing about XML,有没有什么文章介绍?短时间可否学成? 看看XmlDocument这个类,或者到网上查一下. up 用xml。http://www.cnblogs.com/suny2006/archive/2006/12/16/594432.html 按照数据结构里面的方法 线索化以后 保存为LIST类型。 如果不需要保存後人为地查看,我偏向於Serializable。曾今这样保存过一个awt的ui程序,因为要求打开时恢复到记录上一次关闭时界面状态(比如一棵目录树,当前展开了那些子树)。 如果实现了serializable然后直接用ObjectOuputStream.writeObject(Object obj) 读进来的时候用Object ObjectInputStream.readObject() 这个东西比较有意思,特别是写树这些有很多层的组件,非常好用.下面是最简单的把一个JButton的对象写入到硬盘里,然后读取出来,再显示在JFrame上,因为JButton已经实现了Serializable,所以就不用再实现了,自己写了一下,复习一下,好久没弄过了,呵呵:import java.io.*;import javax.swing.*;public class WriteObject { public static void main(String[] args) { ObjectOutputStream oos = null; ObjectInputStream ois = null; try { FileOutputStream fos = new FileOutputStream("obj.dat"); oos = new ObjectOutputStream(fos); JButton button = new JButton("My Button"); oos.writeObject(button); oos.close(); oos = null; FileInputStream fis = new FileInputStream("obj.dat"); ois = new ObjectInputStream(fis); JButton button2 = (JButton)ois.readObject(); ois.close(); ois = null; JFrame frame = new JFrame("Example"); frame.getContentPane().add(button2); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); frame.setVisible(true); } catch(IOException ioex) { System.out.println(ioex.getMessage()); } catch(ClassNotFoundException cnex) { System.out.println(cnex.getMessage()); } finally { try { if (oos != null) oos.close(); if (ois != null) ois.close(); } catch(IOException ioex2) { } } }} 用序列化最省事,但是输出的文件不能阅读;用xml的话方便扩展,jdom例子很多的 如何用java读取电子称的RS232接口上的数据? 请问客户端里判断是不是-1是什么条件 一个简单的浏览器,输入URL以后却不能显示出URL的页面。。 禁止JSP页面内信息删除的问题 JMF的控件如何进行美化? 怎么把linux自带的jdk卸载掉 java throw问题,为什么为什么?抓狂了 如何改观程序的外观 在 Myeclipse 中如何判断一个现有工程的类型 JAVA(JSP)开发困惑-为什么我对jsp看不到希望 解释一下 高手请进
有没有什么文章介绍?
短时间可否学成?
http://www.cnblogs.com/suny2006/archive/2006/12/16/594432.html
然后直接用ObjectOuputStream.writeObject(Object obj)
读进来的时候用Object ObjectInputStream.readObject()
这个东西比较有意思,特别是写树这些有很多层的组件,非常好用.下面是最简单的把一个JButton的对象写入到硬盘里,然后读取出来,再显示在JFrame上,因为JButton已经实现了Serializable,所以就不用再实现了,自己写了一下,复习一下,好久没弄过了,呵呵:
import java.io.*;
import javax.swing.*;public class WriteObject {
public static void main(String[] args) {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
FileOutputStream fos = new FileOutputStream("obj.dat");
oos = new ObjectOutputStream(fos);
JButton button = new JButton("My Button");
oos.writeObject(button);
oos.close();
oos = null;
FileInputStream fis = new FileInputStream("obj.dat");
ois = new ObjectInputStream(fis);
JButton button2 = (JButton)ois.readObject();
ois.close();
ois = null;
JFrame frame = new JFrame("Example");
frame.getContentPane().add(button2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
} catch(IOException ioex) {
System.out.println(ioex.getMessage());
} catch(ClassNotFoundException cnex) {
System.out.println(cnex.getMessage());
} finally {
try {
if (oos != null)
oos.close();
if (ois != null)
ois.close();
} catch(IOException ioex2) {
}
}
}
}