<b>Java swing组件的串行化和读取</b>[Java编程]
本文“<b>Java swing组件的串行化和读取</b>[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
由于JButton和JTree都已经实现了Serializable接口,因此Java swing组件 的串行化和读取是可以做到的.
办法就是利用ObjectInputStream读取文件中的对象,利用 ObjectOutputStream把对象写入文件.
如: 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 javax.swing.JButton;
import javax.swing.JTree;
public class Save {
public static void main(String[] args) {
// Write
JButton button = new JButton("TEST Button");
JTree tree = new JTree();
try {
ObjectOutputStream outForButton = new ObjectOutputStream(
new FileOutputStream("button"));
outForButton.writeObject(button);
outForButton.close();
ObjectOutputStream outForTree = new ObjectOutputStream(
new FileOutputStream("tree"));
outForTree.writeObject(tree);
outForTree.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Read
try {
ObjectInputStream inForButton = new ObjectInputStream(
new FileInputStream("button"));
JButton buttonReaded = (JButton) inForButton.readObject();
ObjectInputStream inForTree = new ObjectInputStream(
new FileInputStream("tree"));
JTree treeReaded = (JTree) inForTree.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上是“<b>Java swing组件的串行化和读取</b>[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |